﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.IO;
using System.Reflection;
using System.Text.RegularExpressions;
//using System.Windows.Forms;
using System.Drawing.Imaging;
using System.Windows.Forms;
using InkFx.WinControl.Utils;

namespace InkFx.WinControl.Core
{
    public static class GraphicsHelper
    {


        #region  绘制图片

        /// <summary>
        /// 调用 System.Windows.Forms.Control 最基础的 背景重绘 (且跳过 各种子类 的 override)
        /// </summary>
        public static void PaintBackground(System.Windows.Forms.Control control, System.Windows.Forms.PaintEventArgs pevent)
        {
            if (control == null || !control.IsHandleCreated || control.IsDisposed) return;

            RECT rect = Win32API.GetClientRect(control.Handle);
            MethodInfo m_MethodInfo_PaintBackground = typeof(System.Windows.Forms.Control).GetMethod("PaintBackground", Win32API.ReflectBindingFlags, (Binder)null, CallingConventions.Any, new Type[] { typeof(System.Windows.Forms.PaintEventArgs), typeof(Rectangle) }, (ParameterModifier[])null);
            if (m_MethodInfo_PaintBackground != null)
                m_MethodInfo_PaintBackground.Invoke(control, new object[] { pevent, new Rectangle(rect.Left, rect.Top, rect.Right, rect.Bottom) });
        }



        public static bool ImageIsDisposed(Image img)
        {
            if (img == null) return true;
            object value = ReflectHelper.GetValue(img, "nativeImage");
            if ((value is IntPtr)) return (((IntPtr)value).ToInt32() <= 0);
            try { return img.Width <= 0; }
            catch { return true; }
        }


        /// <summary>
        /// 从系统屏幕 截取界定控件的 指定区域的 图片, 返回的图片 不会保留 rect 之外的部分 (即, 返回的图片尺寸就是 rect 的尺寸)
        /// <para>rect 的 X Y 是相对于屏幕左上角 的 坐标</para>
        /// </summary>
        public static Image ScreenShot(IntPtr hWnd, Rectangle rect)
        {
            if (hWnd == IntPtr.Zero || rect.Width <= 0 || rect.Height <= 0) return null;

            Bitmap bitmap = new Bitmap(rect.Width, rect.Height);
            using (Graphics g = Graphics.FromImage(bitmap))
            {
                g.CopyFromScreen(rect.X, rect.Y, 0, 0, rect.Size, CopyPixelOperation.SourceCopy);
            }
            return bitmap;
        }


        /// <summary>
        /// 在画布上 的 指定位置 绘制 指定尺寸的图片 (图片会被缩放成指定大小)
        /// </summary>
        public static void DrawImage(Graphics g, Image img, float x, float y, float width, float height)
        {
            if (g == null || img == null || x < short.MinValue || y < short.MinValue || width <= 0 || height <= 0) return;
            g.DrawImage(img, new RectangleF(x, y, width, height), new RectangleF(0, 0, img.Width, img.Height), GraphicsUnit.Pixel);
        }
        /// <summary>
        /// 在画布上 的 指定位置 绘制 指定尺寸的图片 (图片会被缩放成指定大小)
        /// </summary>
        public static void DrawImage(Graphics g, Image img, RectangleF dest)
        {
            if (g == null || img == null || dest.X < short.MinValue || dest.Y < short.MinValue) return;
            g.DrawImage(img, dest, new RectangleF(0, 0, img.Width, img.Height), GraphicsUnit.Pixel);
        }
        /// <summary>
        /// 在画布上 的 指定位置 绘制 指定尺寸的图片 (图片会在绘制时不会被缩放, img 也会按照 x, y 偏移不非绘制区域)
        /// </summary>
        public static void DrawImage(Graphics g, Image img, RectangleF dest, RectangleF src)
        {
            if (g == null || img == null || dest.X < short.MinValue || dest.Y < short.MinValue || src.X < short.MinValue || src.Y < short.MinValue) return;
            g.DrawImage(img, dest, src, GraphicsUnit.Pixel);
        }
        /// <summary>
        /// 在画布上 的 指定位置 居中绘制 指定尺寸的图片 (图片小于目标区域, 则 居中显示; 图片大于目标区域, 则会等比缩小后显示; )
        /// </summary>
        public static void DrawImageCenter(Graphics g, Image img, RectangleF dest)
        {
            DrawImageCenter(g, img, dest, false);
        }
        /// <summary>
        /// 在画布上 的 指定位置 居中绘制 指定尺寸的图片 (最后一个参数 如果为 true: 如果目标尺寸比图片小, 则不绘制)
        /// </summary>
        public static void DrawImageCenter(Graphics g, Image img, RectangleF dest, bool mustSize)
        {
            if (g == null || img == null || dest.X < short.MinValue || dest.Y < short.MinValue) return;

            int imgWidth = img.Width;
            int imgHeight = img.Height;
            if (dest.Width >= imgWidth && dest.Height >= imgHeight)
            {
                int offsetX = (int)(dest.Width - imgWidth) / 2;
                int offsetY = (int)(dest.Height - imgHeight) / 2;
                //g.DrawImage(img, (int)(dest.X + offsetX), (int)(dest.Y + offsetY));
                g.DrawImage(img, new Rectangle((int)(dest.X + offsetX), (int)(dest.Y + offsetY), (int)imgWidth, (int)imgHeight), new Rectangle(0, 0, imgWidth, imgHeight), GraphicsUnit.Pixel);
            }
            else
            {
                if (!mustSize)
                {
                    float width = dest.Width;
                    float height = dest.Height;
                    if (imgWidth >= dest.Width) { width = dest.Width; height = dest.Width / ((float)imgWidth / imgHeight); }
                    else if (imgHeight >= dest.Height) { height = dest.Height; width = dest.Height / ((float)imgHeight / imgWidth); }
                    int offsetX = (int)(dest.Width - width) / 2;
                    int offsetY = (int)(dest.Height - height) / 2;

                    g.DrawImage(img, new Rectangle((int)(dest.X + offsetX), (int)(dest.Y + offsetY), (int)width, (int)height), new Rectangle(0, 0, imgWidth, imgHeight), GraphicsUnit.Pixel);
                }
            }
        }
        /// <summary>
        /// 在画布上 的 指定位置 居中绘制 ImageList中指定索引、指定尺寸的图片 (图片小于目标区域, 则 居中显示; 图片大于目标区域, 则会等比缩小后显示; )
        /// </summary>
        public static void DrawImageListMidImage(System.Windows.Forms.ImageList imageList, Graphics g, Rectangle dest, int imgIndex)
        {
            if (g == null || imgIndex < 0 || dest.X < short.MinValue || dest.Y < short.MinValue) return;

            Size imgSize = imageList.ImageSize;
            int imgWidth = imgSize.Width;
            int imgHeight = imgSize.Height;
            if (dest.Width >= imgWidth && dest.Height >= imgHeight)
            {
                int offsetX = (int)(dest.Width - imgWidth) / 2;
                int offsetY = (int)(dest.Height - imgHeight) / 2;
                imageList.Draw(g, (int)(dest.X + offsetX), (int)(dest.Y + offsetY), imgIndex);
            }
            else
            {
                RectangleF src = new RectangleF(0, 0, imgWidth, imgHeight);

            }
            //todo: 大于 区域时，显示
        }
        /// <summary>
        /// 绘制透明状态图片
        /// </summary>
        public static void DrawAlphaImage(Graphics g, Image img, Rectangle dest, float alpha)
        {
            if (g == null || img == null || dest.X < short.MinValue || dest.Y < short.MinValue) return;

            using (ImageAttributes imageAttributes = new ImageAttributes())
            {
                ColorMap colorMap = new ColorMap();

                colorMap.OldColor = Color.FromArgb(255, 0, 255, 0);
                colorMap.NewColor = Color.FromArgb(0, 0, 0, 0);

                ColorMap[] remapTable = { colorMap };

                imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);

                float[][] colorMatrixElements = { 
                    new float[] {1.0f,  0.0f,  0.0f,  0.0f, 0.0f},       
                    new float[] {0.0f,  1.0f,  0.0f,  0.0f, 0.0f},        
                    new float[] {0.0f,  0.0f,  1.0f,  0.0f, 0.0f},        
                    new float[] {0.0f,  0.0f,  0.0f,  alpha, 0.0f},        
                    new float[] {0.0f,  0.0f,  0.0f,  0.0f, 1.0f}};
                ColorMatrix wmColorMatrix = new ColorMatrix(colorMatrixElements);

                imageAttributes.SetColorMatrix(wmColorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
                g.DrawImage(img, dest, 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, imageAttributes);
            }
        }



        /// <summary>
        /// 在画布上 的 指定位置 纵向拉伸绘制 指定尺寸的图片, 但是 不拉伸上下边框部分
        /// </summary>
        public static void DrawStretchYImage(Graphics g, Image img, float x, float y, float width, float height, int topBorder, int bottomBorder)
        {
            if (g == null || img == null || x < short.MinValue || y < short.MinValue || width <= 0 || height <= 0) return;

            if (topBorder < 0) topBorder = 0;
            if (bottomBorder < 0) bottomBorder = 0;

            int imgWidth = img.Width;
            int imgHeight = img.Height;
            int srcBodyHeight = imgHeight - topBorder - bottomBorder;
            int srcBottomBorderY = imgHeight - bottomBorder;

            float destBodyHeight = height - topBorder - bottomBorder;
            float destBottomBorderY = y + height - bottomBorder;

            //g.DrawImage(img, x, y, width, height);

            g.DrawImage(img, new RectangleF(x, y + topBorder, width, destBodyHeight), new RectangleF(0, topBorder, imgWidth, srcBodyHeight), GraphicsUnit.Pixel);

            if (topBorder > 0)
                g.DrawImage(img, new RectangleF(x, y, width, topBorder), new RectangleF(0, 0, imgWidth, topBorder), GraphicsUnit.Pixel);
            if (bottomBorder > 0)
                g.DrawImage(img, new RectangleF(x, destBottomBorderY, width, bottomBorder), new RectangleF(0, srcBottomBorderY, imgWidth, bottomBorder), GraphicsUnit.Pixel);
        }
        /// <summary>
        /// 在画布上 的 指定位置 横向拉伸绘制 指定尺寸的图片, 但是 不拉伸左右边框部分
        /// </summary>
        public static void DrawStretchXImage(Graphics g, Image img, float x, float y, float width, float height, int leftBorder, int rightBorder)
        {
            if (g == null || img == null || x < short.MinValue || y < short.MinValue || width <= 0 || height <= 0) return;

            if (leftBorder < 0) leftBorder = 0;
            if (rightBorder < 0) rightBorder = 0;

            int imgWidth = img.Width;
            int imgHeight = img.Height;
            int srcBodyWidth = imgWidth - leftBorder - rightBorder;
            int srcBottomBorderX = imgWidth - rightBorder;

            float destBodyWidth = width - leftBorder - rightBorder;
            float destBottomBorderX = x + width - rightBorder;

            //g.DrawImage(img, x, y, width, height);

            g.DrawImage(img, new RectangleF(x + leftBorder, y, destBodyWidth, height), new RectangleF(leftBorder, 0, srcBodyWidth, imgHeight), GraphicsUnit.Pixel);

            if (leftBorder > 0)
                g.DrawImage(img, new RectangleF(x, y, leftBorder, height), new RectangleF(0, 0, leftBorder, imgHeight), GraphicsUnit.Pixel);
            if (rightBorder > 0)
                g.DrawImage(img, new RectangleF(destBottomBorderX, y, rightBorder, height), new RectangleF(srcBottomBorderX, 0, rightBorder, imgHeight), GraphicsUnit.Pixel);
        }
        /// <summary>
        /// 在画布上 的 指定位置 纵向横向拉伸绘制 指定尺寸的图片, 但是 不拉伸上下左右边框部分
        /// </summary>
        public static void DrawStretchXYImage(Graphics g, Image img, float x, float y, float width, float height, int topBorder, int bottomBorder, int leftBorder, int rightBorder)
        {
            if (g == null || img == null || x < short.MinValue || y < short.MinValue || width <= 0 || height <= 0) return;

            if (topBorder < 0) topBorder = 0;
            if (bottomBorder < 0) bottomBorder = 0;
            if (leftBorder < 0) leftBorder = 0;
            if (rightBorder < 0) rightBorder = 0;

            int imgWidth = img.Width;
            int imgHeight = img.Height;

            int srcBodyWidth = imgWidth - leftBorder - rightBorder;
            int srcBodyHeight = imgHeight - topBorder - bottomBorder;
            int srcBottomBorderX = imgWidth - rightBorder;
            int srcBottomBorderY = imgHeight - bottomBorder;

            float destBodyWidth = width - leftBorder - rightBorder;
            float destBodyHeight = height - topBorder - bottomBorder;
            float destBottomBorderX = x + width - rightBorder;
            float destBottomBorderY = y + height - bottomBorder;
            
            //g.DrawImage(img, x, y, width, height);

            g.DrawImage(img, new RectangleF(x + leftBorder, y + topBorder, destBodyWidth, destBodyHeight), new RectangleF(leftBorder, topBorder, srcBodyWidth, srcBodyHeight), GraphicsUnit.Pixel);

            if (topBorder > 0)
                g.DrawImage(img, new RectangleF(x, y, width, topBorder), new RectangleF(0, 0, imgWidth, topBorder), GraphicsUnit.Pixel);
            if (bottomBorder > 0)
                g.DrawImage(img, new RectangleF(x, destBottomBorderY, width, bottomBorder), new RectangleF(0, srcBottomBorderY, imgWidth, bottomBorder), GraphicsUnit.Pixel);
            if (leftBorder > 0)
                g.DrawImage(img, new RectangleF(x, y, leftBorder, height), new RectangleF(0, 0, leftBorder, imgHeight), GraphicsUnit.Pixel);
            if (rightBorder > 0)
                g.DrawImage(img, new RectangleF(destBottomBorderX, y, rightBorder, height), new RectangleF(srcBottomBorderX, 0, rightBorder, imgHeight), GraphicsUnit.Pixel);

        }




        /// <summary>
        /// 在画布上 横向重复 且 纵向重复 绘制 指定图片的部分区域, 直到指定的 宽度、高度
        /// </summary>
        public static void DrawRepeatXYImage(Graphics g, Image img, PointF point, RectangleF imgRect, float width, float height)
        {
            if (g == null || img == null || width <= 0) return;

            float x = point.X;
            float y = point.Y;
            float imgWidth = imgRect.Width;
            float imgHeight = imgRect.Height;


            float dh = 0;
            while (dh < height)
            {
                float ch = (height - dh > imgHeight) ? imgHeight : height - dh;
                if (ch <= 0) break;

                float dw = 0;
                while (dw < width)
                {
                    float cw = (width - dw > imgWidth) ? imgWidth : width - dw;
                    if (cw <= 0) break;

                    g.DrawImage(img, new RectangleF(x + dw, y + dh, cw, ch), imgRect, GraphicsUnit.Pixel);
                    dw = dw + cw;
                }

                dh = dh + ch;
            }
        }

        /// <summary>
        /// 在画布上 横向重复绘制 指定图片, 纵向部分保持原样
        /// </summary>
        public static void DrawRepeatXImage(Graphics g, Image img, PointF point, float width)
        {
            if (g == null || img == null || width <= 0) return;

            float x = point.X;
            float y = point.Y;
            int imgWidth = img.Width;
            int imgHeight = img.Height;

            //g.DrawImage(img, x, y, width, img.Height);

            float dw = 0;
            while (dw < width)
            {
                float cw = (width - dw > imgWidth) ? imgWidth : width - dw;
                if (cw <= 0) break;

                g.DrawImage(img, x + dw, y, cw, imgHeight);
                dw = dw + cw;
            }
        }
        /// <summary>
        /// 在画布上 横向重复绘制 指定图片的部分区域
        /// </summary>
        public static void DrawRepeatXImage(Graphics g, Image img, PointF point, RectangleF imgRect, float width)
        {
            if (g == null || img == null || width <= 0) return;

            float x = point.X;
            float y = point.Y;
            float imgWidth = imgRect.Width;
            float imgHeight = imgRect.Height;

            float dw = 0;
            while (dw < width)
            {
                float cw = (width - dw > imgWidth) ? imgWidth : width - dw;
                if (cw <= 0) break;

                g.DrawImage(img, new RectangleF(x + dw, y, cw, imgHeight), imgRect, GraphicsUnit.Pixel);
                dw = dw + cw;
            }
        }
        /// <summary>
        /// 在画布上 横向重复绘制 指定图片的部分区域, 并且横向拉伸到指定高度
        /// </summary>
        public static void DrawRepeatXImage(Graphics g, Image img, PointF point, RectangleF imgRect, float width, float height)
        {
            if (g == null || img == null || width <= 0) return;

            float x = point.X;
            float y = point.Y;
            float imgWidth = imgRect.Width;
            float imgHeight = imgRect.Height;

            float dw = 0;
            while (dw < width)
            {
                float cw = (width - dw > imgWidth) ? imgWidth : width - dw;
                if (cw <= 0) break;

                g.DrawImage(img, new RectangleF(x + dw, y, cw, height), imgRect, GraphicsUnit.Pixel);
                dw = dw + cw;
            }
        }
        /// <summary>
        /// 在画布上 横向重复绘制 指定图片, 并且在 纵向上进行拉伸, 但是 不拉伸上下边框部分
        /// </summary>
        public static void DrawRepeatXAndStretchYImage(Graphics g, Image img, PointF point, float width, float height, int topBorder, int bottomBorder)
        {
            if (g == null || img == null || width <= 0 || height <= 0) return;

            if (topBorder < 0) topBorder = 0;
            if (bottomBorder < 0) bottomBorder = 0;


            float x = point.X;
            float y = point.Y;
            int imgWidth = img.Width;
            int imgHeight = img.Height;
            int srcBodyHeight = imgHeight - topBorder - bottomBorder;
            int srcBottomBorderY = imgHeight - bottomBorder;

            float destBodyHeight = height - topBorder - bottomBorder;
            float destBottomBorderY = y + height - bottomBorder;


            float dw = 0;
            while (dw < width)
            {
                float cw = (width - dw > imgWidth) ? imgWidth : width - dw;
                if (cw <= 0) break;

                g.DrawImage(img, new RectangleF(x + dw, y + topBorder, cw, destBodyHeight), new RectangleF(0, topBorder, cw, srcBodyHeight), GraphicsUnit.Pixel);

                if (topBorder > 0)
                    g.DrawImage(img, new RectangleF(x + dw, y, cw, topBorder), new RectangleF(0, 0, cw, topBorder), GraphicsUnit.Pixel);
                if (bottomBorder > 0)
                    g.DrawImage(img, new RectangleF(x + dw, destBottomBorderY, cw, bottomBorder), new RectangleF(0, srcBottomBorderY, cw, bottomBorder), GraphicsUnit.Pixel);

                dw = dw + cw;
            }
        }
        /// <summary>
        /// 在画布上 横向重复绘制(不重复 左右边框) 指定图片, 并且在 纵向上进行拉伸, 但是 不拉伸上下边框部分
        /// </summary>
        public static void DrawRepeatXAndStretchYImage(Graphics g, Image img, PointF point, float width, float height, int topBorder, int bottomBorder, int leftBorder, int rightBorder)
        {
            if (g == null || img == null || width <= 0 || height <= 0) return;

            if (topBorder < 0) topBorder = 0;
            if (bottomBorder < 0) bottomBorder = 0;
            if (leftBorder < 0) leftBorder = 0;
            if (rightBorder < 0) rightBorder = 0;

            float x = point.X;
            float y = point.Y;
            int imgWidth = img.Width;
            int imgHeight = img.Height;

            int srcBodyWidth = imgWidth - leftBorder - rightBorder;
            int srcBodyHeight = imgHeight - topBorder - bottomBorder;
            int srcBottomBorderX = imgWidth - rightBorder;
            int srcBottomBorderY = imgHeight - bottomBorder;

            float destBodyWidth = width - leftBorder - rightBorder;
            float destBodyHeight = height - topBorder - bottomBorder;
            float destBottomBorderX = x + width - rightBorder;
            float destBottomBorderY = y + height - bottomBorder;



            if (leftBorder > 0)
                g.DrawImage(img, new RectangleF(x, y, leftBorder, height), new RectangleF(0, 0, leftBorder, imgHeight), GraphicsUnit.Pixel);
            if (rightBorder > 0)
                g.DrawImage(img, new RectangleF(destBottomBorderX, y, rightBorder, height), new RectangleF(0, 0, leftBorder, imgHeight), GraphicsUnit.Pixel);

            float dw = 0;
            while (dw < destBodyWidth)
            {
                float cw = (destBodyWidth - dw > srcBodyWidth) ? srcBodyWidth : destBodyWidth - dw;
                if (cw <= 0) break;

                g.DrawImage(img, new RectangleF(x + dw + leftBorder, y + topBorder, cw, destBodyHeight), new RectangleF(leftBorder, topBorder, cw, srcBodyHeight), GraphicsUnit.Pixel);

                if (topBorder > 0)
                    g.DrawImage(img, new RectangleF(x + dw + leftBorder, y, cw, topBorder), new RectangleF(leftBorder, 0, cw, topBorder), GraphicsUnit.Pixel);
                if (bottomBorder > 0)
                    g.DrawImage(img, new RectangleF(x + dw + leftBorder, destBottomBorderY, cw, bottomBorder), new RectangleF(leftBorder, srcBottomBorderY, cw, bottomBorder), GraphicsUnit.Pixel);

                dw = dw + cw;
            }
        }
        /// <summary>
        /// 在画布上 纵向居中绘制 指定图片的部分区域
        /// </summary>        
        public static void DrawMidXImage(Graphics g, Image img, PointF point, RectangleF imgRect, float width)
        {
            if (g == null || img == null || width <= 0) return;

            float x = point.X;
            float y = point.Y;
            float imgWidth = imgRect.Width;
            float imgHeight = imgRect.Height;

            float cw = (width - imgWidth) / 2;
            g.DrawImage(img, new RectangleF(x + cw, y, imgWidth, imgHeight), imgRect, GraphicsUnit.Pixel);
        }


        /// <summary>
        /// 在画布上 纵向重复绘制 指定图片, 横向部分保持原样
        /// </summary>
        public static void DrawRepeatYImage(Graphics g, Image img, PointF point, float height)
        {
            if (g == null || img == null || height <= 0) return;

            float x = point.X;
            float y = point.Y;
            int imgWidth = img.Width;
            int imgHeight = img.Height;

            float dh = 0;
            while (dh < height)
            {
                float ch = (height - dh > imgHeight) ? imgHeight : height - dh;
                if (ch <= 0) break;

                g.DrawImage(img, x, y + dh, imgWidth, ch);
                dh = dh + ch;
            }
        }
        /// <summary>
        /// 在画布上 纵向重复绘制 指定图片的部分区域
        /// </summary>
        public static void DrawRepeatYImage(Graphics g, Image img, PointF point, RectangleF imgRect, float height)
        {
            if (g == null || img == null || height <= 0) return;

            float x = point.X;
            float y = point.Y;
            float imgWidth = imgRect.Width;
            float imgHeight = imgRect.Height;

            float dh = 0;
            while (dh < height)
            {
                float ch = (height - dh > imgHeight) ? imgHeight : height - dh;
                if (ch <= 0) break;

                g.DrawImage(img, new RectangleF(x, y + dh, imgWidth, ch), imgRect, GraphicsUnit.Pixel);
                dh = dh + ch;
            }
        }
        /// <summary>
        /// 在画布上 纵向重复绘制 指定图片的部分区域, 并且横向拉伸到指定宽度
        /// </summary>
        public static void DrawRepeatYImage(Graphics g, Image img, PointF point, RectangleF imgRect, float height, float width)
        {
            if (g == null || img == null || height <= 0) return;

            float x = point.X;
            float y = point.Y;
            float imgWidth = imgRect.Width;
            float imgHeight = imgRect.Height;

            float dh = 0;
            while (dh < height)
            {
                float ch = (height - dh > imgHeight) ? imgHeight : height - dh;
                if (ch <= 0) break;

                g.DrawImage(img, new RectangleF(x, y + dh, width, ch), imgRect, GraphicsUnit.Pixel);
                dh = dh + ch;
            }
        }
        /// <summary>
        /// 在画布上 纵向重复绘制 指定图片, 并且在 横向上进行拉伸, 但是 不拉伸左右边框部分
        /// </summary>
        public static void DrawRepeatYAndStretchXImage(Graphics g, Image img, PointF point, float height, float width, int leftBorder, int rightBorder)
        {
            if (g == null || img == null || width <= 0 || height <= 0) return;

            if (leftBorder < 0) leftBorder = 0;
            if (rightBorder < 0) rightBorder = 0;


            float x = point.X;
            float y = point.Y;
            int imgWidth = img.Width;
            int imgHeight = img.Height;
            int srcBodyWidth = imgWidth - leftBorder - rightBorder;
            int srcBottomBorderX = imgWidth - rightBorder;

            float destBodyWidth = width - leftBorder - rightBorder;
            float destBottomBorderX = x + width - rightBorder;


            float dh = 0;
            while (dh < height)
            {
                float ch = (height - dh > imgHeight) ? imgHeight : height - dh;
                if (ch <= 0) break;

                g.DrawImage(img, new RectangleF(x + leftBorder, y + dh, destBodyWidth, ch), new RectangleF(leftBorder, 0, srcBodyWidth, ch), GraphicsUnit.Pixel);

                if (leftBorder > 0)
                    g.DrawImage(img, new RectangleF(x, y + dh, leftBorder, ch), new RectangleF(0, 0, leftBorder, ch), GraphicsUnit.Pixel);
                if (rightBorder > 0)
                    g.DrawImage(img, new RectangleF(destBottomBorderX, y + dh, rightBorder, ch), new RectangleF(srcBottomBorderX, 0, rightBorder, ch), GraphicsUnit.Pixel);

                dh = dh + ch;
            }
        }
        /// <summary>
        /// 在画布上 纵向重复绘制(不重复 上下边框) 指定图片, 并且在 横向上进行拉伸, 但是 不拉伸左右边框部分
        /// </summary>
        public static void DrawRepeatYAndStretchXImage(Graphics g, Image img, PointF point, float height, float width, int topBorder, int bottomBorder, int leftBorder, int rightBorder)
        {
            if (g == null || img == null || width <= 0 || height <= 0) return;

            if (topBorder < 0) topBorder = 0;
            if (bottomBorder < 0) bottomBorder = 0;
            if (leftBorder < 0) leftBorder = 0;
            if (rightBorder < 0) rightBorder = 0;

            float x = point.X;
            float y = point.Y;
            int imgWidth = img.Width;
            int imgHeight = img.Height;

            int srcBodyWidth = imgWidth - leftBorder - rightBorder;
            int srcBodyHeight = imgHeight - topBorder - bottomBorder;
            int srcBottomBorderX = imgWidth - rightBorder;
            int srcBottomBorderY = imgHeight - bottomBorder;

            float destBodyWidth = width - leftBorder - rightBorder;
            float destBodyHeight = height - topBorder - bottomBorder;
            float destBottomBorderX = x + width - rightBorder;
            float destBottomBorderY = y + height - bottomBorder;



            if (topBorder > 0)
                g.DrawImage(img, new RectangleF(x, y, width, leftBorder), new RectangleF(0, 0, imgWidth, topBorder), GraphicsUnit.Pixel);
            if (bottomBorder > 0)
                g.DrawImage(img, new RectangleF(x, destBottomBorderY, width, bottomBorder), new RectangleF(0, 0, imgWidth, topBorder), GraphicsUnit.Pixel);

            float dh = 0;
            while (dh < destBodyHeight)
            {
                float ch = (destBodyHeight - dh > srcBodyHeight) ? srcBodyHeight : destBodyHeight - dh;
                if (ch <= 0) break;

                g.DrawImage(img, new RectangleF(x + leftBorder, y + dh + topBorder, destBodyWidth, ch), new RectangleF(leftBorder, topBorder, srcBodyWidth, ch), GraphicsUnit.Pixel);

                if (leftBorder > 0)
                    g.DrawImage(img, new RectangleF(x, y + dh + topBorder, leftBorder, ch), new RectangleF(0, topBorder, leftBorder, ch), GraphicsUnit.Pixel);
                if (rightBorder > 0)
                    g.DrawImage(img, new RectangleF(destBottomBorderX, y + dh + topBorder, rightBorder, ch), new RectangleF(srcBottomBorderX, topBorder, rightBorder, ch), GraphicsUnit.Pixel);

                dh = dh + ch;
            }

        }
        /// <summary>
        /// 在画布上 纵向居中绘制 指定图片的部分区域
        /// </summary>
        public static void DrawMidYImage(Graphics g, Image img, Point point, RectangleF imgRect, float height)
        {
            if (g == null || img == null || height <= 0) return;

            int x = point.X;
            int y = point.Y;
            float imgWidth = imgRect.Width;
            float imgHeight = imgRect.Height;

            float ch = (height - imgHeight) / 2;
            g.DrawImage(img, new RectangleF(x, y + ch, imgWidth, imgHeight), imgRect, GraphicsUnit.Pixel);
        }


        /// <summary>
        /// 在画布上 的 指定位置 绘制 指定图片的 边框区域 (本函数用法 和 DrawStretchXYImage 基本一致, 唯一不同的是 本函数不会 绘制 边框内部的 图片)
        /// </summary>
        public static void DrawBorderImage(Graphics g, Image img, float x, float y, float width, float height, int topBorder, int bottomBorder, int leftBorder, int rightBorder)
        {
            if (g == null || img == null || x < short.MinValue || y < short.MinValue || width <= 0 || height <= 0) return;

            if (topBorder < 0) topBorder = 0;
            if (bottomBorder < 0) bottomBorder = 0;
            if (leftBorder < 0) leftBorder = 0;
            if (rightBorder < 0) rightBorder = 0;

            int imgWidth = img.Width;
            int imgHeight = img.Height;



            int srcBodyWidth = imgWidth - leftBorder - rightBorder;
            int srcBodyHeight = imgHeight - topBorder - bottomBorder;
            int srcBottomBorderX = imgWidth - rightBorder;
            int srcBottomBorderY = imgHeight - bottomBorder;

            float destBodyWidth = width - leftBorder - rightBorder;
            float destBodyHeight = height - topBorder - bottomBorder;
            float destBottomBorderX = x + width - rightBorder;
            float destBottomBorderY = y + height - bottomBorder;


            #region  绘制 四个顶点

            if (topBorder > 0 && leftBorder > 0)
            {
                g.DrawImage(img, new RectangleF(x, y, leftBorder, topBorder), new Rectangle(0, 0, leftBorder, topBorder), GraphicsUnit.Pixel);
            }
            if (topBorder > 0 && rightBorder > 0)
            {
                g.DrawImage(img, new RectangleF(destBottomBorderX, y, rightBorder, topBorder), new Rectangle(srcBottomBorderX, 0, rightBorder, topBorder), GraphicsUnit.Pixel);
            }
            if (bottomBorder > 0 && leftBorder > 0)
            {
                g.DrawImage(img, new RectangleF(x, destBottomBorderY, leftBorder, bottomBorder), new Rectangle(0, srcBottomBorderY, leftBorder, bottomBorder), GraphicsUnit.Pixel);
            }
            if (bottomBorder > 0 && rightBorder > 0)
            {
                g.DrawImage(img, new RectangleF(destBottomBorderX, destBottomBorderY, rightBorder, bottomBorder), new Rectangle(srcBottomBorderX, srcBottomBorderY, rightBorder, bottomBorder), GraphicsUnit.Pixel);
            }

            #endregion

            #region  绘制 四个边框

            if (topBorder > 0)
            {
                DrawRepeatXImage(g, img, new PointF(leftBorder, 0), new RectangleF(leftBorder, 0, imgWidth - leftBorder - rightBorder, topBorder), destBodyWidth);
            }
            if (bottomBorder > 0)
            {
                DrawRepeatXImage(g, img, new PointF(leftBorder, destBottomBorderY), new Rectangle(leftBorder, srcBottomBorderY, imgWidth - leftBorder - rightBorder, bottomBorder), destBodyWidth);
            }
            if (leftBorder > 0)
            {
                DrawRepeatYImage(g, img, new PointF(0, topBorder), new Rectangle(0, topBorder, leftBorder, imgHeight - topBorder - bottomBorder), destBodyHeight);
            }
            if (rightBorder > 0)
            {
                DrawRepeatYImage(g, img, new PointF(destBottomBorderX, topBorder), new Rectangle(srcBottomBorderX, topBorder, rightBorder, imgHeight - topBorder - bottomBorder), destBodyHeight);
            }

            #endregion
        }
        /// <summary>
        /// 在画布上 的 指定位置 绘制 指定图片的 边框区域, 并且 按照指定的 规则 填充内部区域, 当目标尺寸比原始图片边框还小时, 图片将会在整体上自动缩放 (本函数用法 和 DrawStretchXYImage 基本一致)
        /// </summary>
        public static void DrawBorderImageAndFill(Graphics g, Image img, float x, float y, float width, float height, int topBorder, int bottomBorder, int leftBorder, int rightBorder, FillStyle fillStyle)
        {
            if (g == null || img == null || x < short.MinValue || y < short.MinValue || width <= 0 || height <= 0) return;

            if (topBorder < 0) topBorder = 0;
            if (bottomBorder < 0) bottomBorder = 0;
            if (leftBorder < 0) leftBorder = 0;
            if (rightBorder < 0) rightBorder = 0;


            if (img.Width == (int)Math.Round(width) && img.Height == (int)Math.Round(height))
            {
                DrawImage(g, img, x, y, width, height);
            }
            else
            {
                //Image tempImg = CreateImageBorderAndFill(img, width, height, topBorder, bottomBorder, leftBorder, rightBorder, fillStyle);
                //using (tempImg) g.DrawImage(tempImg, new Rectangle(x, y, width, height), new Rectangle(0, 0, tempImg.Width, tempImg.Height), GraphicsUnit.Pixel);

                DrawImageBorderAndFill(g, img, new RectangleF(x, y, width, height), topBorder, bottomBorder, leftBorder, rightBorder, fillStyle);
            }
        }

        /// <summary>
        /// 在画布上 的 指定位置 绘制 指定颜色的 边框区域
        /// </summary>
        public static void DrawBorder(Graphics g, Color color, float x, float y, float width, float height, int topBorder, int bottomBorder, int leftBorder, int rightBorder)
        {
            if (IsEmptyColor(color)) return;
            if (g == null/* || color == Color.Transparent*/  || x < short.MinValue || y < short.MinValue || width <= 0 || height <= 0) return;

            if (topBorder < 0) topBorder = 0;
            if (bottomBorder < 0) bottomBorder = 0;
            if (leftBorder < 0) leftBorder = 0;
            if (rightBorder < 0) rightBorder = 0;

            using(Brush brush = new SolidBrush(color))
            {
                if (topBorder > 0)
                {
                    g.FillRectangle(brush, 0, 0, width, topBorder);
                }
                if (bottomBorder > 0)
                {
                    g.FillRectangle(brush, 0, height - bottomBorder, width, bottomBorder);
                }
                if (leftBorder > 0)
                {
                    g.FillRectangle(brush, 0, topBorder, leftBorder, height - topBorder - bottomBorder);
                }
                if (rightBorder > 0)
                {
                    g.FillRectangle(brush, width - rightBorder, topBorder, rightBorder, height - topBorder - bottomBorder);
                }
            }
        }
        /// <summary>
        /// 在画布上 的 指定位置 绘制 指定颜色的 边框区域 (参数 correction 如果为 true, 则实际绘制的 矩形区域长宽会减少 1像素)
        /// </summary>
        public static void DrawBorder(Graphics g, Color color, RectangleF rect, bool correction, bool enable)
        {
            if (IsEmptyColor(color)) return;
            if (g == null/* || color == Color.Transparent*/  || rect.X < short.MinValue || rect.X < short.MinValue || rect.Width <= 0 || rect.Height <= 0) return;

            if (!enable) color = GraphicsHelper.LightLight(color);
            if (correction) { rect.Width = rect.Width - 1; rect.Height = rect.Height - 1; }
            using (Pen pen = new Pen(color)) g.DrawRectangle(pen, rect.X, rect.Y, rect.Width, rect.Height);
        }


        public static void FillBack(Graphics g, Color backColor, bool enable)
        {
            if (IsEmptyColor(backColor)) return;
            if (!enable) backColor = GraphicsHelper.LightLight(backColor);
            g.Clear(backColor);
        }
        public static void FillBack(Graphics g, RectangleF rect, Color backColor, bool enable)
        {
            if (IsEmptyColor(backColor)) return;
            if (rect.Width <= 0 || rect.Height <= 0) return;

            if (!enable) backColor = GraphicsHelper.LightLight(backColor);
            using (SolidBrush brush = new SolidBrush(backColor)) g.FillRectangle(brush, rect);
        }
        public static void FillBack(Graphics g, Region region, Color backColor, bool enable)
        {
            if (IsEmptyColor(backColor)) return;
            if (region == null) return;

            if (!enable) backColor = GraphicsHelper.LightLight(backColor);
            using (SolidBrush brush = new SolidBrush(backColor)) g.FillRegion(brush, region);
        }

        public static void FillBackHLinear(Graphics g, RectangleF rect, Color backColor0, Color backColor1, bool enable)
        {
            if (rect.Width <= 0 || rect.Height <= 0) return;
            if (IsEmptyColor(backColor0) && IsEmptyColor(backColor1)) return;

            if (!enable)
            {
                backColor0 = GraphicsHelper.LightLight(backColor0);
                backColor1 = GraphicsHelper.LightLight(backColor1);
            }
            using (LinearGradientBrush brush = new LinearGradientBrush(rect, backColor0, backColor1, LinearGradientMode.Horizontal))
                g.FillRectangle(brush, rect);
        }
        public static void FillBackVLinear(Graphics g, RectangleF rect, Color backColor0, Color backColor1, bool enable)
        {
            if (rect.Width <= 0 || rect.Height <= 0) return;
            if (IsEmptyColor(backColor0) && IsEmptyColor(backColor1)) return;

            if (!enable)
            {
                backColor0 = GraphicsHelper.LightLight(backColor0);
                backColor1 = GraphicsHelper.LightLight(backColor1);
            }
            using (LinearGradientBrush brush = new LinearGradientBrush(rect, backColor0, backColor1, LinearGradientMode.Vertical))
                g.FillRectangle(brush, rect);
        }
        public static void FillBackHLinear(Graphics g, RectangleF rect, Color[] colors, float[] positions, bool enable)
        {
            if (rect.Width <= 0 || rect.Height <= 0) return;
            if (colors == null || colors.Length <= 0) return;
            if (IsEmptyColors(colors)) return;

            if (!enable)
            {
                for (int i = 0, count = colors.Length; i < count; i++)
                    colors[i] = GraphicsHelper.LightLight(colors[i]);
            }
            using (LinearGradientBrush brush = new LinearGradientBrush(rect, Color.Transparent, Color.Transparent, LinearGradientMode.Horizontal))
            {
                ColorBlend blend = new ColorBlend();
                blend.Positions = positions; //new float[] { 0.0f, basePosition, basePosition + 0.05f, 1.0f };
                blend.Colors = colors;
                brush.InterpolationColors = blend;
                g.FillRectangle(brush, rect);
            }
        }
        public static void FillBackVLinear(Graphics g, RectangleF rect, Color[] colors, float[] positions, bool enable)
        {
            if (rect.Width <= 0 || rect.Height <= 0) return;
            if (colors == null || colors.Length <= 0) return;
            if (IsEmptyColors(colors)) return;

            if (!enable)
            {
                for (int i = 0, count = colors.Length; i < count; i++)
                    colors[i] = GraphicsHelper.LightLight(colors[i]);
            }
            using (LinearGradientBrush brush = new LinearGradientBrush(rect, Color.Transparent, Color.Transparent, LinearGradientMode.Vertical))
            {
                ColorBlend blend = new ColorBlend();
                blend.Positions = positions; //new float[] { 0.0f, basePosition, basePosition + 0.05f, 1.0f };
                blend.Colors = colors;
                brush.InterpolationColors = blend;
                g.FillRectangle(brush, rect);
            }
        }





        public static Color Light(Color color)
        {
            if (color == Color.Transparent || color.IsEmpty) return Color.Transparent;
            //return System.Windows.Forms.ControlPaint.Light(color);
            return DarkLightColor(color, 0.25f);
        }
        public static Color LightLight(Color color)
        {
            if (color == Color.Transparent || color.IsEmpty) return Color.Transparent;
            //return System.Windows.Forms.ControlPaint.LightLight(color);
            return DarkLightColor(color, 0.5f);
        }
        public static Color Dark(Color color)
        {
            if (color == Color.Transparent || color.IsEmpty) return Color.Transparent;
            //return System.Windows.Forms.ControlPaint.Dark(color);
            return DarkLightColor(color, -0.25f);
        }
        public static Color DarkDark(Color color)
        {
            if (color == Color.Transparent || color.IsEmpty) return Color.Transparent;
            //return System.Windows.Forms.ControlPaint.DarkDark(color);
            return DarkLightColor(color, -0.5f);
        }
        public static Color Light(Color color, float ratio)
        {
            if (color == Color.Transparent || color.IsEmpty) return Color.Transparent;
            //return System.Windows.Forms.ControlPaint.Light(color, percOfLightLight);
            return DarkLightColor(color, ratio);
        }
        public static Color Dark(Color color, float ratio)
        {
            if (color == Color.Transparent || color.IsEmpty) return Color.Transparent;
            //return System.Windows.Forms.ControlPaint.Dark(color, percOfDarkDark);
            return DarkLightColor(color, 0 - ratio);
        }
        public static Color DarkLightColor(Color color, float ratio)
        {
            if (Math.Abs(ratio - 0) < 0.0000001) return color;

            float r = color.R;
            float g = color.G;
            float b = color.B;

            if (ratio > 0)
            {
                r = r + (255 - r) * ratio;
                g = g + (255 - g) * ratio;
                b = b + (255 - b) * ratio;
            }
            else
            {
                r = r + r * ratio;
                g = g + g * ratio;
                b = b + b * ratio;
            }

            r = Math.Min(255, Math.Max(0, r));
            g = Math.Min(255, Math.Max(0, g));
            b = Math.Min(255, Math.Max(0, b));
            return Color.FromArgb(color.A, (int)r, (int)g, (int)b);
        }



        /// <summary>
        /// 创建指定图像的灰度 副本
        /// </summary>
        public static Image CreateDisabledImage(Image img)
        {
            return CreateDisabledImage(img, SystemColors.Control);
        }
        /// <summary>
        /// 创建指定图像的灰度 副本
        /// </summary>
        public static Image CreateDisabledImage(Image img, Color backColor)
        {
            if (img == null) return null;

            return System.Windows.Forms.ToolStripRenderer.CreateDisabledImage(img);

            Bitmap bitmap = new Bitmap(img.Width, img.Height);
            using (Graphics g = Graphics.FromImage(bitmap))
                System.Windows.Forms.ControlPaint.DrawImageDisabled(g, img, 0, 0, backColor);
            return bitmap;
        }
        /// <summary>
        /// 将指定的图片 纵向均分为 count 份,并返回 索引为 index 的 那一部分图片
        /// </summary>
        public static Image SplitYImage(Image img, int count, int index)
        {
            if (img == null) return null;

            if (count <= 0) count = 1;

            int width = img.Width;
            int height = img.Height / count;
            int x = 0;
            int y = height * index;

            Bitmap bitmap = new Bitmap(width, height);
            using (Graphics g = Graphics.FromImage(bitmap))
            {
                g.DrawImage(img, new Rectangle(0, 0, width, height), new Rectangle(x, y, width, height), GraphicsUnit.Pixel);
            }
            return bitmap;
        }
        /// <summary>
        /// 将指定的图片 纵向均分为 count 份,并返回 索引为 index 的 那一部分图片, 最后返回的 图片, 在边界上 有 额外的加成.
        /// </summary>
        public static Image SplitYImage(Image img, int count, int index, int topOffset, int bottomOffset, int leftOffset, int rightOffset)
        {
            if (img == null) return null;

            if (count <= 0) count = 1;

            int width = img.Width;
            int height = img.Height / count;
            int x = 0;
            int y = height * index;

            Bitmap bitmap = new Bitmap(width + leftOffset + rightOffset, height + topOffset + bottomOffset);
            using (Graphics g = Graphics.FromImage(bitmap))
            {
                //g.Clear(Color.Black);
                g.DrawImage(img, new Rectangle(leftOffset, topOffset, width, height), new Rectangle(x, y, width, height), GraphicsUnit.Pixel);
            }
            return bitmap;
        }

        /// <summary>
        /// 得到一个图片, 并 指定位置 绘制 指定图片的 边框区域, 并且 按照指定的 规则 填充内部区域, 当目标尺寸比原始图片边框还小时, 图片将会在整体上自动缩放 (本函数用法 和 DrawStretchXYImage 基本一致)
        /// </summary>
        [Obsolete("通过创建临时图片的重绘将导致性能降低, 不建议使用本函数", false)]
        public static Image CreateImageBorderAndFill(Image img, float width, float height, BorderOffset borderOffset, FillStyle fillStyle)
        {
            return CreateImageBorderAndFill(img, width, height, borderOffset.Top, borderOffset.Bottom, borderOffset.Left, borderOffset.Right, fillStyle);
        }
        /// <summary>
        /// 得到一个图片, 并 指定位置 绘制 指定图片的 边框区域, 并且 按照指定的 规则 填充内部区域, 当目标尺寸比原始图片边框还小时, 图片将会在整体上自动缩放 (本函数用法 和 DrawStretchXYImage 基本一致)
        /// </summary>
        [Obsolete("通过创建临时图片的重绘将导致性能降低, 不建议使用本函数", false)]
        public static Image CreateImageBorderAndFill(Image img, float width, float height, int topBorder, int bottomBorder, int leftBorder, int rightBorder, FillStyle fillStyle)
        {
            if (img == null) return null;
            if (width <= 0 || height <= 0) return null;

            if (topBorder < 0) topBorder = 0;
            if (bottomBorder < 0) bottomBorder = 0;
            if (leftBorder < 0) leftBorder = 0;
            if (rightBorder < 0) rightBorder = 0;

            int imgWidth = img.Width;
            int imgHeight = img.Height;

            int srcBodyWidth = imgWidth - leftBorder - rightBorder;
            int srcBodyHeight = imgHeight - topBorder - bottomBorder;
            int srcRightBorderX = imgWidth - rightBorder;
            int srcBottomBorderY = imgHeight - bottomBorder;

            float destBodyWidth = width - leftBorder - rightBorder;
            float destBodyHeight = height - topBorder - bottomBorder;
            float destRightBorderX = 0 + width - rightBorder;
            float destBottomBorderY = 0 + height - bottomBorder;

            float tempWidth = Math.Max(width, leftBorder + rightBorder);      //临时 图片宽度
            float tempHeight = Math.Max(height, topBorder + bottomBorder);    //临时 图片高度

            Bitmap tempImg = new Bitmap((int)tempWidth, (int)tempHeight);
            using (Graphics tempG = Graphics.FromImage(tempImg))
            {
                #region  绘制 四个顶点

                if (topBorder > 0 && leftBorder > 0)
                {
                    tempG.DrawImage(img, new RectangleF(0, 0, leftBorder, topBorder), new RectangleF(0, 0, leftBorder, topBorder), GraphicsUnit.Pixel);
                }
                if (topBorder > 0 && rightBorder > 0)
                {
                    tempG.DrawImage(img, new RectangleF(destRightBorderX, 0, rightBorder, topBorder), new RectangleF(srcRightBorderX, 0, rightBorder, topBorder), GraphicsUnit.Pixel);
                }
                if (bottomBorder > 0 && leftBorder > 0)
                {
                    tempG.DrawImage(img, new RectangleF(0, destBottomBorderY, leftBorder, bottomBorder), new RectangleF(0, srcBottomBorderY, leftBorder, bottomBorder), GraphicsUnit.Pixel);
                }
                if (bottomBorder > 0 && rightBorder > 0)
                {
                    tempG.DrawImage(img, new RectangleF(destRightBorderX, destBottomBorderY, rightBorder, bottomBorder), new RectangleF(srcRightBorderX, srcBottomBorderY, rightBorder, bottomBorder), GraphicsUnit.Pixel);
                }

                #endregion

                #region  绘制 四个边框

                if (topBorder > 0)
                {
                    if (fillStyle == FillStyle.RepeatXY || fillStyle == FillStyle.RepeatXAndStretchY || fillStyle == FillStyle.StretchXAndMidY)
                    {
                        DrawRepeatXImage(tempG, img, new PointF(leftBorder, 0), new RectangleF(leftBorder, 0, srcBodyWidth, topBorder), destBodyWidth);
                    }
                    else if (fillStyle == FillStyle.StretchYAndMidX)
                    {
                        int midXWdith = imgWidth - leftBorder - rightBorder;
                        float destLeftBorder = (width - midXWdith) / 2;
                        DrawRepeatXImage(tempG, img, new PointF(destLeftBorder, 0), new RectangleF(leftBorder, 0, srcBodyWidth, topBorder), midXWdith);
                    }
                    else
                        DrawImage(tempG, img, new RectangleF(leftBorder, 0, destBodyWidth, topBorder), new RectangleF(leftBorder, 0, srcBodyWidth, topBorder));
                }
                if (bottomBorder > 0)
                {
                    if (fillStyle == FillStyle.RepeatXY || fillStyle == FillStyle.RepeatXAndStretchY || fillStyle == FillStyle.StretchXAndMidY)
                    {
                        DrawRepeatXImage(tempG, img, new PointF(leftBorder, destBottomBorderY), new RectangleF(leftBorder, srcBottomBorderY, srcBodyWidth, bottomBorder), destBodyWidth);
                    }                    
                    else if (fillStyle == FillStyle.StretchYAndMidX)
                    {
                        int midXWdith = imgWidth - leftBorder - rightBorder;
                        float destLeftBorder = (width - midXWdith) / 2;
                        DrawRepeatXImage(tempG, img, new PointF(destLeftBorder, destBottomBorderY), new RectangleF(leftBorder, srcBottomBorderY, srcBodyWidth, bottomBorder), midXWdith);
                    }
                    else
                        DrawImage(tempG, img, new RectangleF(leftBorder, destBottomBorderY, destBodyWidth, bottomBorder), new Rectangle(leftBorder, srcBottomBorderY, srcBodyWidth, bottomBorder));
                }
                if (leftBorder > 0)
                {
                    if (fillStyle == FillStyle.RepeatXY || fillStyle == FillStyle.RepeatYAndStretchX || fillStyle == FillStyle.StretchYAndMidX)
                    {
                        DrawRepeatYImage(tempG, img, new PointF(0, topBorder), new RectangleF(0, topBorder, leftBorder, srcBodyHeight), destBodyHeight);
                    }
                    else if (fillStyle == FillStyle.StretchXAndMidY)
                    {
                        int midYHeight = imgHeight - topBorder - bottomBorder;
                        float destTopBorder = (height - midYHeight) / 2;
                        DrawRepeatYImage(tempG, img, new PointF(0, destTopBorder), new RectangleF(0, topBorder, leftBorder, srcBodyHeight), midYHeight);
                    }
                    else
                        DrawImage(tempG, img, new RectangleF(0, topBorder, leftBorder, destBodyHeight), new RectangleF(0, topBorder, leftBorder, srcBodyHeight));
                }
                if (rightBorder > 0)
                {
                    if (fillStyle == FillStyle.RepeatXY || fillStyle == FillStyle.RepeatYAndStretchX || fillStyle == FillStyle.StretchYAndMidX)
                    {
                        DrawRepeatYImage(tempG, img, new PointF(destRightBorderX, topBorder), new RectangleF(srcRightBorderX, topBorder, rightBorder, srcBodyHeight), destBodyHeight);
                    }                    
                    else if (fillStyle == FillStyle.StretchXAndMidY)
                    {
                        int midYHeight = imgHeight - topBorder - bottomBorder;
                        float destTopBorder = (height - midYHeight) / 2;
                        DrawRepeatYImage(tempG, img, new PointF(destRightBorderX, destTopBorder), new RectangleF(srcRightBorderX, topBorder, rightBorder, srcBodyHeight), midYHeight);
                    }
                    else
                        DrawImage(tempG, img, new RectangleF(destRightBorderX, topBorder, rightBorder, destBodyHeight), new RectangleF(srcRightBorderX, topBorder, rightBorder, srcBodyHeight));
                }

                #endregion

                #region  填充 内部区域

                if (fillStyle == FillStyle.Fill)
                {
                    tempG.DrawImage(img, new RectangleF(leftBorder, topBorder, destBodyWidth, destBodyHeight), new RectangleF(leftBorder, topBorder, srcBodyWidth, srcBodyHeight), GraphicsUnit.Pixel);
                }
                else if (fillStyle == FillStyle.RepeatXAndStretchY)
                {
                    DrawRepeatXImage(tempG, img, new PointF(leftBorder, topBorder), new RectangleF(leftBorder, topBorder, srcBodyWidth, srcBodyHeight), destBodyWidth, destBodyHeight);
                }
                else if (fillStyle == FillStyle.RepeatYAndStretchX)
                {
                    DrawRepeatYImage(tempG, img, new PointF(leftBorder, topBorder), new RectangleF(leftBorder, topBorder, srcBodyWidth, srcBodyHeight), destBodyHeight, destBodyWidth);
                }
                else if (fillStyle == FillStyle.StretchXAndMidY)
                {
                    int midYHeight = imgHeight - topBorder - bottomBorder;
                    float destTopBorder = (height - midYHeight) / 2;
                    float destBttomBorder = height - midYHeight - topBorder;
                    tempG.DrawImage(img, new RectangleF(leftBorder, destTopBorder, destBodyWidth, midYHeight), new RectangleF(leftBorder, topBorder, srcBodyWidth, srcBodyHeight), GraphicsUnit.Pixel);
                }
                else if (fillStyle == FillStyle.StretchYAndMidX)
                {
                    int midXWdith = imgWidth - leftBorder - rightBorder;
                    float destLeftBorder = (width - midXWdith) / 2;
                    float destRightBorder = width - midXWdith - leftBorder;
                    tempG.DrawImage(img, new RectangleF(destLeftBorder, topBorder, midXWdith, destBodyHeight), new RectangleF(leftBorder, topBorder, srcBodyWidth, srcBodyHeight), GraphicsUnit.Pixel);
                }
                else if (fillStyle == FillStyle.RepeatXY)
                {
                    DrawRepeatXYImage(tempG, img, new Point(leftBorder, topBorder), new Rectangle(leftBorder, topBorder, srcBodyWidth, srcBodyHeight), destBodyWidth, destBodyHeight);
                }
                else if (fillStyle == FillStyle.None)
                {
                    //什么都不做
                }

                #endregion
            }

            return tempImg;

            //#region  返回 最后的结果

            //if (tempImg.Width == width && tempImg.Height == height)
            //{
            //    return tempImg;
            //}
            //else
            //{
            //    using (tempImg)
            //    {
            //        Bitmap resultImg = new Bitmap(width, height);
            //        using (Graphics resultG = Graphics.FromImage(resultImg))
            //        {
            //            resultG.DrawImage(tempImg, new Rectangle(0, 0, width, height), new Rectangle(0, 0, tempImg.Width, tempImg.Height), GraphicsUnit.Pixel);
            //        }
            //        return resultImg;
            //    }
            //}

            //#endregion
        }
        /// <summary>
        /// 将一个图片按照边框值进行缩放, 并且 按照指定的 规则 填充内部区域, 当目标尺寸比原始图片边框还小时, 图片将会在整体上自动缩放 (本函数用法 和 DrawStretchXYImage 基本一致)
        /// </summary>
        public static void DrawImageBorderAndFill(Graphics g, Image img, RectangleF destRect, BorderOffset borderOffset, FillStyle fillStyle)
        {
            DrawImageBorderAndFill(g, img, destRect, borderOffset.Top, borderOffset.Bottom, borderOffset.Left, borderOffset.Right, fillStyle);
        }
        /// <summary>
        /// 将一个图片按照边框值进行缩放, 并且 按照指定的 规则 填充内部区域, 当目标尺寸比原始图片边框还小时, 图片将会在整体上自动缩放 (本函数用法 和 DrawStretchXYImage 基本一致)
        /// </summary>
        public static void DrawImageBorderAndFill(Graphics g, Image img, RectangleF destRect, int topBorder, int bottomBorder, int leftBorder, int rightBorder, FillStyle fillStyle)
        {
            if (g == null || img == null) return;
            if (destRect.Width <= 0 || destRect.Height <= 0) return;

            if (topBorder < 0) topBorder = 0;
            if (bottomBorder < 0) bottomBorder = 0;
            if (leftBorder < 0) leftBorder = 0;
            if (rightBorder < 0) rightBorder = 0;

            float x = destRect.X; //2; //
            float y = destRect.Y; //2; //
            float width = destRect.Width; //destRect.Width - 4; //
            float height = destRect.Height; //destRect.Height - 4; //
            int imgWidth = img.Width;
            int imgHeight = img.Height;

            int srcBodyWidth = imgWidth - leftBorder - rightBorder;
            int srcBodyHeight = imgHeight - topBorder - bottomBorder;
            int srcRightBorderX = imgWidth - rightBorder;
            int srcBottomBorderY = imgHeight - bottomBorder;

            float destBodyWidth = width - leftBorder - rightBorder;
            float destBodyHeight = height - topBorder - bottomBorder;
            float destRightBorderX = 0 + width - rightBorder;
            float destBottomBorderY = 0 + height - bottomBorder;

            //int tempWidth = Math.Max(width, leftBorder + rightBorder);      //临时 图片宽度
            //int tempHeight = Math.Max(height, topBorder + bottomBorder);    //临时 图片高度

            #region  绘制 四个顶点

            if (topBorder > 0 && leftBorder > 0)
            {
                g.DrawImage(img, new RectangleF(x, y, leftBorder, topBorder), new RectangleF(0, 0, leftBorder, topBorder), GraphicsUnit.Pixel);
            }
            if (topBorder > 0 && rightBorder > 0)
            {
                g.DrawImage(img, new RectangleF(x + destRightBorderX, y, rightBorder, topBorder), new RectangleF(srcRightBorderX, 0, rightBorder, topBorder), GraphicsUnit.Pixel);
            }
            if (bottomBorder > 0 && leftBorder > 0)
            {
                g.DrawImage(img, new RectangleF(x, y + destBottomBorderY, leftBorder, bottomBorder), new RectangleF(0, srcBottomBorderY, leftBorder, bottomBorder), GraphicsUnit.Pixel);
            }
            if (bottomBorder > 0 && rightBorder > 0)
            {
                g.DrawImage(img, new RectangleF(x + destRightBorderX, y + destBottomBorderY, rightBorder, bottomBorder), new RectangleF(srcRightBorderX, srcBottomBorderY, rightBorder, bottomBorder), GraphicsUnit.Pixel);
            }

            #endregion

            #region  绘制 四个边框

            if (topBorder > 0)
            {
                if (fillStyle == FillStyle.RepeatXY || fillStyle == FillStyle.RepeatXAndStretchY || fillStyle == FillStyle.StretchXAndMidY)
                {
                    DrawRepeatXImage(g, img, new PointF(x + leftBorder, y), new RectangleF(leftBorder, 0, srcBodyWidth, topBorder), destBodyWidth);
                }
                else if (fillStyle == FillStyle.StretchYAndMidX)
                {
                    int midXWdith = imgWidth - leftBorder - rightBorder;
                    float destLeftBorder = (width - midXWdith) / 2;
                    DrawRepeatXImage(g, img, new PointF(x + destLeftBorder, y), new RectangleF(leftBorder, 0, srcBodyWidth, topBorder), midXWdith);
                }
                else
                    DrawImage(g, img, new RectangleF(x + leftBorder, y, destBodyWidth, topBorder), new RectangleF(leftBorder, 0, srcBodyWidth, topBorder));
            }
            if (bottomBorder > 0)
            {
                if (fillStyle == FillStyle.RepeatXY || fillStyle == FillStyle.RepeatXAndStretchY || fillStyle == FillStyle.StretchXAndMidY)
                {
                    DrawRepeatXImage(g, img, new PointF(x + leftBorder, y + destBottomBorderY), new RectangleF(leftBorder, srcBottomBorderY, srcBodyWidth, bottomBorder), destBodyWidth);
                }
                else if (fillStyle == FillStyle.StretchYAndMidX)
                {
                    int midXWdith = imgWidth - leftBorder - rightBorder;
                    float destLeftBorder = (width - midXWdith) / 2;
                    DrawRepeatXImage(g, img, new PointF(x + destLeftBorder, y + destBottomBorderY), new RectangleF(leftBorder, srcBottomBorderY, srcBodyWidth, bottomBorder), midXWdith);
                }
                else
                    DrawImage(g, img, new RectangleF(x + leftBorder, y + destBottomBorderY, destBodyWidth, bottomBorder), new Rectangle(leftBorder, srcBottomBorderY, srcBodyWidth, bottomBorder));
            }
            if (leftBorder > 0)
            {
                if (fillStyle == FillStyle.RepeatXY || fillStyle == FillStyle.RepeatYAndStretchX || fillStyle == FillStyle.StretchYAndMidX)
                {
                    DrawRepeatYImage(g, img, new PointF(x, y + topBorder), new RectangleF(0, topBorder, leftBorder, srcBodyHeight), destBodyHeight);
                }
                else if (fillStyle == FillStyle.StretchXAndMidY)
                {
                    int midYHeight = imgHeight - topBorder - bottomBorder;
                    float destTopBorder = (height - midYHeight) / 2;
                    DrawRepeatYImage(g, img, new PointF(x, y + destTopBorder), new RectangleF(0, topBorder, leftBorder, srcBodyHeight), midYHeight);
                }
                else
                    DrawImage(g, img, new RectangleF(x, y + topBorder, leftBorder, destBodyHeight), new RectangleF(0, topBorder, leftBorder, srcBodyHeight));
            }
            if (rightBorder > 0)
            {
                if (fillStyle == FillStyle.RepeatXY || fillStyle == FillStyle.RepeatYAndStretchX || fillStyle == FillStyle.StretchYAndMidX)
                {
                    DrawRepeatYImage(g, img, new PointF(x + destRightBorderX, y + topBorder), new RectangleF(srcRightBorderX, topBorder, rightBorder, srcBodyHeight), destBodyHeight);
                }
                else if (fillStyle == FillStyle.StretchXAndMidY)
                {
                    int midYHeight = imgHeight - topBorder - bottomBorder;
                    float destTopBorder = (height - midYHeight) / 2;
                    DrawRepeatYImage(g, img, new PointF(x + destRightBorderX, y + destTopBorder), new RectangleF(srcRightBorderX, topBorder, rightBorder, srcBodyHeight), midYHeight);
                }
                else
                    DrawImage(g, img, new RectangleF(x + destRightBorderX, y + topBorder, rightBorder, destBodyHeight), new RectangleF(srcRightBorderX, topBorder, rightBorder, srcBodyHeight));
            }

            #endregion

            #region  填充 内部区域

            if (fillStyle == FillStyle.Fill)
            {
                g.DrawImage(img, new RectangleF(x + leftBorder, y + topBorder, destBodyWidth, destBodyHeight), new RectangleF(leftBorder, topBorder, srcBodyWidth, srcBodyHeight), GraphicsUnit.Pixel);
            }
            else if (fillStyle == FillStyle.RepeatXAndStretchY)
            {
                DrawRepeatXImage(g, img, new PointF(x + leftBorder, y + topBorder), new RectangleF(leftBorder, topBorder, srcBodyWidth, srcBodyHeight), destBodyWidth, destBodyHeight);
            }
            else if (fillStyle == FillStyle.RepeatYAndStretchX)
            {
                DrawRepeatYImage(g, img, new PointF(x + leftBorder, y + topBorder), new RectangleF(leftBorder, topBorder, srcBodyWidth, srcBodyHeight), destBodyHeight, destBodyWidth);
            }
            else if (fillStyle == FillStyle.StretchXAndMidY)
            {
                int midYHeight = imgHeight - topBorder - bottomBorder;
                float destTopBorder = (height - midYHeight) / 2;
                float destBttomBorder = height - midYHeight - topBorder;
                g.DrawImage(img, new RectangleF(x + leftBorder, y + destTopBorder, destBodyWidth, midYHeight), new RectangleF(leftBorder, topBorder, srcBodyWidth, srcBodyHeight), GraphicsUnit.Pixel);
            }
            else if (fillStyle == FillStyle.StretchYAndMidX)
            {
                int midXWdith = imgWidth - leftBorder - rightBorder;
                float destLeftBorder = (width - midXWdith) / 2;
                float destRightBorder = width - midXWdith - leftBorder;
                g.DrawImage(img, new RectangleF(x + destLeftBorder, y + topBorder, midXWdith, destBodyHeight), new RectangleF(leftBorder, topBorder, srcBodyWidth, srcBodyHeight), GraphicsUnit.Pixel);
            }
            else if (fillStyle == FillStyle.RepeatXY)
            {
                DrawRepeatXYImage(g, img, new PointF(x + leftBorder, y + topBorder), new RectangleF(leftBorder, topBorder, srcBodyWidth, srcBodyHeight), destBodyWidth, destBodyHeight);
            }
            else if (fillStyle == FillStyle.None)
            {
                //什么都不做
            }

            #endregion
        }


        /// <summary>
        /// 合并 背景图 和 中心图, 得到指定 尺寸的图片, 在 背景图 转换成 指定尺寸时, 你可以使用 最后的 两个参数, 确定背景图 的切割边界 和 内部填充 模式
        /// </summary>
        [Obsolete("通过创建临时图片的重绘将导致性能降低, 不建议使用本函数", false)]
        public static Image CreateImageMergeBackAndCenterImage(Image imgBack, Image imgCenter, float width, float height, BorderOffset borderOffset, FillStyle fillStyle)
        {
            return CreateImageMergeBackAndCenterImage(imgBack, imgCenter, width, height, borderOffset.Top, borderOffset.Bottom, borderOffset.Left, borderOffset.Right, fillStyle);
        }
        /// <summary>
        /// 合并 背景图 和 中心图, 得到指定 尺寸的图片, 在 背景图 转换成 指定尺寸时, 你可以使用 最后的 五个参数, 确定背景图 的切割边界 和 内部填充 模式
        /// </summary>
        [Obsolete("通过创建临时图片的重绘将导致性能降低, 不建议使用本函数", false)]
        public static Image CreateImageMergeBackAndCenterImage(Image imgBack, Image imgCenter, float width, float height, int topBorder, int bottomBorder, int leftBorder, int rightBorder, FillStyle fillStyle)
        {
            if (imgBack == null && imgCenter == null) return null;
            if (width <= 0 || height <= 0) return null;

            Bitmap bitmap = new Bitmap((int)width, (int)height);
            using (Graphics g = Graphics.FromImage(bitmap))
            {
                Image newBackImg = imgBack == null 
                    ? null 
                    : CreateImageBorderAndFill(imgBack, width, height, topBorder, bottomBorder, leftBorder, rightBorder, fillStyle);

                if (newBackImg != null)
                    using (newBackImg)
                    {
                        g.DrawImage(newBackImg, new RectangleF(0, 0, newBackImg.Width, newBackImg.Height), new RectangleF(0, 0, newBackImg.Width, newBackImg.Height), GraphicsUnit.Pixel);
                    }

                if (imgCenter != null)
                {
                    g.DrawImage(imgCenter, new RectangleF((width - imgCenter.Width) / 2, (height - imgCenter.Height) / 2, (int)imgCenter.Width, (int)imgCenter.Height), new RectangleF(0, 0, imgCenter.Width, imgCenter.Height), GraphicsUnit.Pixel);
                }
            }

            return bitmap;
        }


        /// <summary>
        /// 按照指定的颜色 创建一个 指定尺寸的 图片
        /// </summary>
        public static Image CreateImageByColor(int width, int height, Color color)
        {
            if (width <= 0 || height <= 0) return null;
            Bitmap tempImg = new Bitmap(width, height);
            using (Graphics tempG = Graphics.FromImage(tempImg))
                tempG.Clear(color);
            return tempImg;
        }
        /// <summary>
        /// 按照指定的颜色 创建一个 指定尺寸的 渐变图片
        /// </summary>
        public static Image CreateImageByColor(int width, int height, Color[] colors, float[] colorPositions, LinearGradientMode mode)
        {
            if (width <= 0 || height <= 0) return null;
            Bitmap tempImg = new Bitmap(width, height);
            using (Graphics tempG = Graphics.FromImage(tempImg))
            {
                Rectangle rect = new Rectangle(0, 0, width, height);
                using (LinearGradientBrush brush = new LinearGradientBrush(rect, Color.Transparent, Color.Transparent, mode))
                {
                    ColorBlend blend = new ColorBlend();
                    blend.Positions = colorPositions;
                    blend.Colors = colors;
                    brush.InterpolationColors = blend;

                    //using (new AntiAliasGraphics(tempG))
                    {
                        tempG.FillRectangle(brush, rect);
                    }
                }
            }
            return tempImg;
        }

        /// <summary>
        /// 将指定图片 旋转指定的方向
        /// </summary>
        public static Image CreateImageByRotate(Image img, RotateAngle angle)
        {
            if (img == null || img.Width <= 0 || img.Height <= 0) return null;
            Image tempImg = CreateImageByRotate(img, (int)angle);
            return tempImg;
        }
        /// <summary>
        /// 将指定图片 旋转指定的角度
        /// </summary>
        public static Image CreateImageByRotate(Image img, int angle)
        {
            if (img == null || img.Width <= 0 || img.Height <= 0) return null;

            angle = angle % 360;

            //弧度转换  
            double radian = angle * Math.PI / 180.0;
            double cos = Math.Cos(radian);
            double sin = Math.Sin(radian);


            //原图的宽和高  
            int w = img.Width;
            int h = img.Height;
            int W = (int)(Math.Max(Math.Abs(w * cos - h * sin), Math.Abs(w * cos + h * sin)));
            int H = (int)(Math.Max(Math.Abs(w * sin - h * cos), Math.Abs(w * sin + h * cos)));


            //目标位图  
            Bitmap tempImg = new Bitmap(W, H);
            using (Graphics tempG = Graphics.FromImage(tempImg))
            {
                //tempG.InterpolationMode = InterpolationMode.Bilinear;
                //tempG.SmoothingMode = SmoothingMode.HighQuality;
                
                //计算偏移量  
                Point Offset = new Point((W - w) / 2, (H - h) / 2);
                //构造图像显示区域：让图像的中心与窗口的中心点一致  
                Rectangle rect = new Rectangle(Offset.X, Offset.Y, w, h);
                Point center = new Point(rect.X + rect.Width / 2, rect.Y + rect.Height / 2);
                tempG.TranslateTransform(center.X, center.Y);
                tempG.RotateTransform(360 - angle);

                //恢复图像在水平和垂直方向的平移  
                tempG.TranslateTransform(-center.X, -center.Y);
                tempG.DrawImage(img, rect);
                //重至绘图的所有变换  
                tempG.ResetTransform();
            }

            return tempImg;
        }  


        /// <summary>
        /// 创建一个 指定图片的 副本
        /// </summary>
        public static Image CloneImage(Image image)
        {
            if (image == null) return null;
            try
            {
                Bitmap bitmap = new Bitmap(image.Width, image.Height);
                using (Graphics g = Graphics.FromImage(bitmap))
                    g.DrawImage(image, 0, 0, image.Width, image.Height);
                return bitmap;
            }
            catch { return null; }
        }

        #endregion


        #region  绘制文字

        /// <summary>
        /// 获取指定的文本颜色, 则 禁用状态下 的 颜色副本
        /// </summary>
        public static Color DisabledForeColor(Color foreColor, bool enable)
        {
            if (!enable) foreColor = GraphicsHelper.LightLight(foreColor);
            return foreColor;
        }

        /// <summary>
        /// 在指定矩形内正中心 绘制文本 (会增加省略号, 仅支持横向)
        /// </summary>
        /// <param name="g">画布</param>
        /// <param name="s">文本</param>
        /// <param name="font">字体</param>
        /// <param name="foreColor">字符串颜色</param>
        /// <param name="rect">局部矩形</param>
        /// <param name="enable">是否是启用状态</param>
        public static void DrawStringCenter(Graphics g, string s, Font font, Color foreColor, RectangleF rect, bool enable)
        {
            if (rect.Width <= 2) return;
            using (StringFormat strFormat = new StringFormat(StringFormat.GenericDefault))
            {
                strFormat.LineAlignment = StringAlignment.Near;
                strFormat.Trimming = StringTrimming.EllipsisCharacter;

                //// 求取字符串大小
                //SizeF size = g.MeasureString(s, font);
                // 绘制旋转后文本
                //using (new SmoothingModeGraphics(g))
                {
                    int leftSpace = LeftSpaceCount(s);
                    int rightSpace = RightSpaceCount(s);

                    SizeF sizeCenter = g.MeasureString(s.Replace(' ', '0'), font);
                    SizeF sizeRight = leftSpace <= 0 ? sizeCenter : g.MeasureString(s.Trim() + new string('0', rightSpace), font);
                    SizeF sizeText = g.MeasureString(s.Trim(), font); //g.MeasureString(*) 自带 Trim(), 即 s 前后如果有空格的话, 返回的结果将不包括空格的长度
                    if (!enable) foreColor = GraphicsHelper.LightLight(foreColor);

                    using (SolidBrush brush = new SolidBrush(foreColor))
                    {
                        int offsetX = Math.Max(0, (int)Math.Ceiling((rect.Width - sizeCenter.Width) / 2));
                        int offsetY = (int)Math.Ceiling((rect.Height - sizeCenter.Height) / 2);

                        RectangleF rectCenter = new RectangleF(rect.X + offsetX, rect.Y + offsetY, sizeCenter.Width, sizeCenter.Height);
                        RectangleF rectText = new RectangleF(rectCenter.X + (sizeCenter.Width - sizeRight.Width), rectCenter.Y, Math.Min(sizeText.Width, rect.Width), sizeText.Height);
                        g.DrawString(s.Trim(), font, brush, rectText, strFormat);
                        //TextRenderer.DrawText((IDeviceContext)g, s, font, new Rectangle((int)rectText.X, (int)rectText.Y, (int)rectText.Width, (int)rectText.Height), foreColor);
                    }
                    //TextRenderer.DrawText(g, s, font, new Point((int)point.X, (int)point.Y), Color.Red);
                    //ControlPaint.DrawStringDisabled(g, s, font, foreColor, new RectangleF(rect.X, rect.Y + (rect.Height - sizeText.Height) / 2, sizeText.Width, sizeText.Height), StringFormat.GenericDefault);
                }
            }
        }
        /// <summary>
        /// 在指定矩形内靠左纵向居中 绘制文本 (会增加省略号, 仅支持横向)
        /// </summary>
        /// <param name="g">画布</param>
        /// <param name="s">文本</param>
        /// <param name="font">字体</param>
        /// <param name="foreColor">字符串颜色</param>
        /// <param name="rect">局部矩形</param>
        /// <param name="enable">是否是启用状态</param>
        public static void DrawStringMiddle(Graphics g, string s, Font font, Color foreColor, RectangleF rect, bool enable)
        {
            if (rect.Width <= 2) return;
            using (StringFormat strFormat = new StringFormat(StringFormat.GenericDefault))
            {
                strFormat.LineAlignment = StringAlignment.Near;
                strFormat.Trimming = StringTrimming.EllipsisCharacter;

                //// 求取字符串大小
                //SizeF size = g.MeasureString(s, font);
                // 绘制旋转后文本
                //using (new SmoothingModeGraphics(g))
                {
                    int leftSpace = LeftSpaceCount(s);
                    int rightSpace = RightSpaceCount(s);

                    SizeF sizeCenter = g.MeasureString(s.Replace(' ', '0'), font);
                    SizeF sizeRight = leftSpace <= 0 ? sizeCenter : g.MeasureString(s.Trim() + new string('0', rightSpace), font);
                    SizeF sizeText = g.MeasureString(s.Trim(), font); //g.MeasureString(*) 自带 Trim(), 即 s 前后如果有空格的话, 返回的结果将不包括空格的长度
                    if (!enable) foreColor = GraphicsHelper.LightLight(foreColor);

                    using (SolidBrush brush = new SolidBrush(foreColor))
                    {
                        int offsetY = (int)Math.Ceiling((rect.Height - sizeCenter.Height) / 2);
                        RectangleF rectCenter = new RectangleF(rect.X, rect.Y + offsetY, sizeCenter.Width, sizeCenter.Height);
                        RectangleF rectText = new RectangleF(rectCenter.X + (sizeCenter.Width - sizeRight.Width), rectCenter.Y, Math.Min(sizeText.Width, rect.Width), sizeText.Height);
                        g.DrawString(s.Trim(), font, brush, rectText, strFormat);
                        //TextRenderer.DrawText((IDeviceContext)g, s, font, new Rectangle((int)rectText.X, (int)rectText.Y, (int)rectText.Width, (int)rectText.Height), foreColor);
                    }
                    //ControlPaint.DrawStringDisabled(g, s, font, foreColor, new RectangleF(rect.X, rect.Y + (rect.Height - sizeText.Height) / 2, sizeText.Width, sizeText.Height), StringFormat.GenericDefault);
                }
            }
        }

        /// <summary>
        /// 在指定矩形内 绘制文本 (会增加省略号, 仅支持横向)
        /// </summary>
        /// <param name="g">画布</param>
        /// <param name="s">文本</param>
        /// <param name="font">字体</param>
        /// <param name="foreColor">字符串颜色</param>
        /// <param name="rect">局部矩形</param>
        /// <param name="enable">是否是启用状态</param>
        public static void DrawString(Graphics g, string s, Font font, Color foreColor, RectangleF rect, bool enable)
        {
            if (rect.Width <= 2) return;
            s = FormatCharAnd(s);

            //// 求取字符串大小
            //SizeF size = g.MeasureString(s, font);
            // 绘制旋转后文本
            //using (new SmoothingModeGraphics(g))
            {
                SizeF sizeText = g.MeasureString(s + "  ", font);
                //Size sizeText = GraphicsHelper.GetStringPixel(s, font);
                if (!enable) foreColor = GraphicsHelper.LightLight(foreColor);

                int offsetY = (int)Math.Ceiling((rect.Height - sizeText.Height) / 2);
                using (SolidBrush brush = new SolidBrush(foreColor))
                    g.DrawString(s, font, brush, new RectangleF(rect.X, rect.Y + offsetY, sizeText.Width, sizeText.Height), StringFormat.GenericDefault);
                //TextRenderer.DrawText(g, s, font, new Point((int)point.X, (int)point.Y), Color.Red);
                //ControlPaint.DrawStringDisabled(g, s, font, foreColor, new RectangleF(rect.X, rect.Y + (rect.Height - sizeText.Height) / 2, sizeText.Width, sizeText.Height), StringFormat.GenericDefault);
            }
        }
        /// <summary>
        /// 在指定矩形内 绘制文本 (会增加省略号, 仅支持横向), 并且会在 文本下方1像素 的位置 绘制一条直线
        /// </summary>
        /// <param name="g">画布</param>
        /// <param name="s">文本</param>
        /// <param name="font">字体</param>
        /// <param name="foreColor">字符串颜色</param>
        /// <param name="rect">局部矩形</param>
        /// <param name="format">布局方式</param>
        /// <param name="enable">是否是启用状态</param>
        public static void DrawStringAndBottomLine(Graphics g, string s, Font font, Color foreColor, RectangleF rect, StringFormat format, bool enable)
        {
            if (rect.Width <= 2) return;
            s = FormatCharAnd(s);

            //// 求取字符串大小
            //SizeF size = g.MeasureString(s, font);
            // 绘制旋转后文本
            //using (new SmoothingModeGraphics(g))
            {
                SizeF sizeText = g.MeasureString(s, font);
                //Size sizeText = GraphicsHelper.GetStringPixel(s, font);
                if (!enable) foreColor = GraphicsHelper.LightLight(foreColor);

                int offsetY = (int)Math.Ceiling((rect.Height - sizeText.Height) / 2);
                RectangleF rectText = new RectangleF(rect.X, rect.Y + offsetY - 1, sizeText.Width, sizeText.Height);
                using (SolidBrush brush = new SolidBrush(foreColor))
                {
                    g.DrawString(s, font, brush, rectText, format);
                }

                using (Pen pen = new Pen(foreColor))
                {
                    g.DrawLine(pen, rectText.X + 2, rectText.Bottom - 2, rectText.Right - 5, rectText.Bottom - 2);
                }

                //TextRenderer.DrawText(g, s, font, new Point((int)point.X, (int)point.Y), Color.Red);
                //ControlPaint.DrawStringDisabled(g, s, font, foreColor, new RectangleF(rect.X, rect.Y + (rect.Height - sizeText.Height) / 2, sizeText.Width, sizeText.Height), StringFormat.GenericDefault);
            }
        }

        /// <summary>
        /// 绘制根据矩形旋转文本
        /// </summary>
        /// <param name="g">画布</param>
        /// <param name="s">文本</param>
        /// <param name="font">字体</param>
        /// <param name="foreColor">字符串颜色</param>
        /// <param name="rect">局部矩形</param>
        /// <param name="format">布局方式</param>
        /// <param name="angle">角度</param>
        /// <param name="enable">是否是启用状态</param>
        public static void DrawString(Graphics g, string s, Font font, Color foreColor, RectangleF rect, bool enable, int angle)
        {
            if (rect.Width <= 2) return;
            s = FormatCharAnd(s);

            //求取字符串大小
            SizeF size = GetStringPixelF(g, s, font);

            //根据旋转后尺寸、布局矩形、布局方式计算文本旋转点
            PointF rotatePt = GetRotatePoint(rect, StringFormat.GenericDefault);

            //重设布局方式都为Center
            StringFormat format2 = new StringFormat(StringFormat.GenericDefault);
            format2.Alignment = StringAlignment.Center;
            format2.LineAlignment = StringAlignment.Center;
            format2.Trimming = StringTrimming.EllipsisCharacter;

            //绘制旋转后文本
            DrawString(g, s, font, foreColor, rotatePt, rect, format2, enable, angle);
            format2.Dispose();
        }

        /// <summary>
        /// 绘制根据点旋转文本，一般旋转点给定位文本包围盒中心点
        /// </summary>
        /// <param name="g">画布</param>
        /// <param name="s">文本</param>
        /// <param name="font">字体</param>
        /// <param name="foreColor">字符串颜色</param>
        /// <param name="point">旋转点</param>
        /// <param name="rect">局部矩形</param>
        /// <param name="format">布局方式</param>
        /// <param name="angle">角度</param>
        /// <param name="enable">是否是启用状态</param>
        public static void DrawString(Graphics g, string s, Font font, Color foreColor, PointF point, RectangleF rect, StringFormat format, bool enable, float angle)
        {
            if (rect.Width <= 2) return;

            s = FormatCharAnd(s);
            if (!enable) foreColor = GraphicsHelper.LightLight(foreColor);

            Matrix bak = g.Transform;
            try
            {
                using(Matrix mtx = g.Transform)
                {
                    mtx.RotateAt(angle, point);
                    g.Transform = mtx;

                    using (SolidBrush brush = new SolidBrush(foreColor))
                        g.DrawString(s, font, brush, point, format);
                }
            }
            finally { g.Transform = bak; }
        }




        /// <summary>
        /// 去掉 字符串中 表示 下划线 的 &amp; 符号
        /// </summary>
        public static string FormatCharAnd(string s)
        {
            try { s = Regex.Replace(s, @"\&(?=\w)", string.Empty, (RegexOptions.Compiled | RegexOptions.IgnoreCase)); }
            catch (Exception ex) { Tools.LogWarn(ex); }
            return s;
        }

        /// <summary>
        /// 获取一个字符串 在指定 字体下的 横向尺寸
        /// </summary>
        public static Size GetStringPixel(string s, Font font)
        {
            Size size = System.Windows.Forms.TextRenderer.MeasureText(s, font);
            return size;
        }        
        /// <summary>
        /// 获取一个字符串 在指定 字体下的 横向尺寸
        /// </summary>
        public static Size GetStringPixel(Graphics g, string s, Font font)
        {
            SizeF size = g.MeasureString(s.Replace(' ', '0'), font);
            return new Size((int)Math.Ceiling(size.Width), (int)Math.Ceiling(size.Height));
        }
        /// <summary>
        /// 获取一个字符串 在指定 字体下的 横向尺寸
        /// </summary>
        public static SizeF GetStringPixelF(Graphics g, string s, Font font)
        {
            SizeF size = g.MeasureString(s.Replace(' ', '0'), font);
            return size;
        }

        /// <summary>
        /// 通过布局枚举 得到指定尺寸 在 父级区域 参照下的 子区域
        /// </summary>
        public static Rectangle GetRectByAlign(Rectangle rect, Size size, ContentAlignment align)
        {
            if (rect == Rectangle.Empty) return Rectangle.Empty;
            
            if (align == ContentAlignment.BottomCenter)
                return new Rectangle(rect.X + (rect.Width - size.Width) / 2, rect.Y + (rect.Height - size.Height), size.Width, size.Height);
            else if (align == ContentAlignment.BottomLeft)
                return new Rectangle(rect.X, rect.Y + (rect.Height - size.Height), size.Width, size.Height);
            else if (align == ContentAlignment.BottomRight)
                return new Rectangle(rect.X + (rect.Width - size.Width), rect.Y + (rect.Height - size.Height), size.Width, size.Height);
            else if (align == ContentAlignment.MiddleCenter)
                return new Rectangle(rect.X + (rect.Width - size.Width) / 2, rect.Y + (rect.Height - size.Height) / 2, size.Width, size.Height);
            else if (align == ContentAlignment.MiddleLeft)
                return new Rectangle(rect.X, rect.Y + (rect.Height - size.Height) / 2, size.Width, size.Height);
            else if (align == ContentAlignment.MiddleRight)
                return new Rectangle(rect.X + (rect.Width - size.Width), rect.Y + (rect.Height - size.Height) / 2, size.Width, size.Height);
            else if (align == ContentAlignment.TopCenter)
                return new Rectangle(rect.X + (rect.Width - size.Width) / 2, rect.Y, size.Width, size.Height);
            else if (align == ContentAlignment.TopLeft)
                return new Rectangle(rect.X, rect.Y, size.Width, size.Height);
            else if (align == ContentAlignment.TopRight)
                return new Rectangle(rect.X + (rect.Width - size.Width), rect.Y, size.Width, size.Height);

            return Rectangle.Empty;
        }

        /// <summary>
        /// 获取一个字符串左边的半角空格数
        /// </summary>
        public static int LeftSpaceCount(string s)
        {
            if (string.IsNullOrEmpty(s)) return 0;
            int result = 0;
            for(int i=0,c=s.Length;i<c;i++)
                if (s[i] == ' ') result++;
                else break;
            return result;
        }
        /// <summary>
        /// 获取一个字符串右边的半角空格数
        /// </summary>
        public static int RightSpaceCount(string s)
        {
            if (string.IsNullOrEmpty(s)) return 0;
            int result = 0;
            for (int i = s.Length-1; i >=0; i--)
                if (s[i] == ' ') result++;
                else break;
            return result;
        }

        #endregion


        #region  绘制线段

        public static void DrawLines(Graphics g, Color color, Point[] points)
        {
            if (g == null) return;
            using (Pen pen = new Pen(color)) g.DrawLines(pen, points);
        }


        #endregion




        #region  颜色控制

        public static bool IsEmptyColor(Color color)
        {
            return color.A <= 0 || color == Color.Transparent || color.IsEmpty;
        }
        public static bool IsEmptyColors(IEnumerable<Color> colors)
        {
            if (colors == null) return true;

            foreach (Color color in colors)
                if (!IsEmptyColor(color)) return false;
            return true;
        }
        public static Color GetRandomColor()
        {
            Random random = new Random(Guid.NewGuid().GetHashCode());
            int a = random.Next(1, 255);
            int g = random.Next(1, 255);
            int b = random.Next(1, 255);

            return Color.FromArgb(a, g, b);
        }

        #endregion


        #region  其他函数

        /// <summary>
        /// 获取鼠标 相对窗体 的 坐标
        /// </summary>
        public static Point GetMouseFormPoint(Form form)
        {
            Point screenPoint = System.Windows.Forms.Control.MousePosition;
            Point formLocation = form.Location;
            Point formPoint = new Point(screenPoint.X - formLocation.X, screenPoint.Y - formLocation.Y);
            return formPoint;
        }
        /// <summary>
        /// 获取鼠标 相对控件 的 坐标
        /// </summary>
        public static Point GetMouseCtrlPoint(System.Windows.Forms.Control ctrl)
        {
            Point screenPoint = System.Windows.Forms.Control.MousePosition;
            Point ctrlPoint = ctrl.PointToClient(screenPoint);
            return ctrlPoint;
            //RECT rect = Win32API.GetWindowRect(ctrl.Handle);
            //Point ctrlPoint = new Point(screenPoint.X - rect.X, screenPoint.Y - rect.Y);
            //return ctrlPoint;
        }

        /// <summary>
        /// 通过控件 当前的状态, 得到 控件指定区域 应该呈现的 V3图片的绘制状态 (虽然本函数, 能够判断 鼠标状态, 但是 这个参数依然由 第三、第四个参数外部提供)
        /// </summary>
        public static V3ImgState GetV3ImgState(System.Windows.Forms.Control control, RectangleF rect, bool mouseEnter, bool mouseDown)
        {
            if (control == null) return V3ImgState.Normal;
            if (!control.Enabled) return V3ImgState.Disable;

            if (rect.Width <= 0 || rect.Height <= 0) return V3ImgState.Normal;

            Point mousePoint = GetMouseCtrlPoint(control);
            if (rect.Contains(mousePoint))
            {
                if (mouseDown) return V3ImgState.Pressed;
                if (mouseEnter) return V3ImgState.Hover;
            }

            return V3ImgState.Normal;
        }



        #endregion





        #region  ImageDrawRect

        public static ContentAlignment anyRight = ContentAlignment.BottomRight | (ContentAlignment.MiddleRight | ContentAlignment.TopRight);
        public static ContentAlignment anyTop = ContentAlignment.TopRight | (ContentAlignment.TopCenter | ContentAlignment.TopLeft);
        public static ContentAlignment anyBottom = ContentAlignment.BottomRight | (ContentAlignment.BottomCenter | ContentAlignment.BottomLeft);
        public static ContentAlignment anyCenter = ContentAlignment.BottomCenter | (ContentAlignment.MiddleCenter | ContentAlignment.TopCenter);
        public static ContentAlignment anyMiddle = ContentAlignment.MiddleRight | (ContentAlignment.MiddleCenter | ContentAlignment.MiddleLeft);
        /// <summary>
        /// 绘图对像
        /// </summary>
        /// <param name="g">绘图对像</param>
        /// <param name="img">图片</param>
        /// <param name="r">绘置的图片大小、坐标</param>
        /// <param name="lr">绘置的图片边界</param>
        /// <param name="index">当前状态</param> 
        /// <param name="Totalindex">状态总数</param>
        public static void DrawRect(Graphics g, Bitmap img, Rectangle r, Rectangle lr, int index, int Totalindex)
        {
            if (img == null) return;
            Rectangle r1, r2;
            int x = (index - 1) * img.Width / Totalindex;
            int y = 0;
            int x1 = r.Left;
            int y1 = r.Top;

            if (r.Height > img.Height && r.Width <= img.Width / Totalindex)
            {
                r1 = new Rectangle(x, y, img.Width / Totalindex, lr.Top);
                r2 = new Rectangle(x1, y1, r.Width, lr.Top);
                g.DrawImage(img, r2, r1, GraphicsUnit.Pixel);

                r1 = new Rectangle(x, y + lr.Top, img.Width / Totalindex, img.Height - lr.Top - lr.Bottom);
                r2 = new Rectangle(x1, y1 + lr.Top, r.Width, r.Height - lr.Top - lr.Bottom);
                if ((lr.Top + lr.Bottom) == 0) r1.Height = r1.Height - 1;
                g.DrawImage(img, r2, r1, GraphicsUnit.Pixel);

                r1 = new Rectangle(x, y + img.Height - lr.Bottom, img.Width / Totalindex, lr.Bottom);
                r2 = new Rectangle(x1, y1 + r.Height - lr.Bottom, r.Width, lr.Bottom);
                g.DrawImage(img, r2, r1, GraphicsUnit.Pixel);
            }
            else
                if (r.Height <= img.Height && r.Width > img.Width / Totalindex)
                {
                    r1 = new Rectangle(x, y, lr.Left, img.Height);
                    r2 = new Rectangle(x1, y1, lr.Left, r.Height);
                    g.DrawImage(img, r2, r1, GraphicsUnit.Pixel);
                    r1 = new Rectangle(x + lr.Left, y, img.Width / Totalindex - lr.Left - lr.Right, img.Height);
                    r2 = new Rectangle(x1 + lr.Left, y1, r.Width - lr.Left - lr.Right, r.Height);
                    g.DrawImage(img, r2, r1, GraphicsUnit.Pixel);
                    r1 = new Rectangle(x + img.Width / Totalindex - lr.Right, y, lr.Right, img.Height);
                    r2 = new Rectangle(x1 + r.Width - lr.Right, y1, lr.Right, r.Height);
                    g.DrawImage(img, r2, r1, GraphicsUnit.Pixel);
                }
                else
                    if (r.Height <= img.Height && r.Width <= img.Width / Totalindex)
                    {
                        r1 = new Rectangle((index - 1) * img.Width / Totalindex, 0, img.Width / Totalindex, img.Height - 1);
                        g.DrawImage(img, new Rectangle(x1, y1, r.Width, r.Height), r1, GraphicsUnit.Pixel);
                    }
                    else if (r.Height > img.Height && r.Width > img.Width / Totalindex)
                    {
                        //top-left
                        r1 = new Rectangle(x, y, lr.Left, lr.Top);
                        r2 = new Rectangle(x1, y1, lr.Left, lr.Top);
                        g.DrawImage(img, r2, r1, GraphicsUnit.Pixel);

                        //top-bottom
                        r1 = new Rectangle(x, y + img.Height - lr.Bottom, lr.Left, lr.Bottom);
                        r2 = new Rectangle(x1, y1 + r.Height - lr.Bottom, lr.Left, lr.Bottom);
                        g.DrawImage(img, r2, r1, GraphicsUnit.Pixel);

                        //left
                        r1 = new Rectangle(x, y + lr.Top, lr.Left, img.Height - lr.Top - lr.Bottom);
                        r2 = new Rectangle(x1, y1 + lr.Top, lr.Left, r.Height - lr.Top - lr.Bottom);
                        g.DrawImage(img, r2, r1, GraphicsUnit.Pixel);

                        //top
                        r1 = new Rectangle(x + lr.Left, y,
                            img.Width / Totalindex - lr.Left - lr.Right, lr.Top);
                        r2 = new Rectangle(x1 + lr.Left, y1,
                            r.Width - lr.Left - lr.Right, lr.Top);
                        g.DrawImage(img, r2, r1, GraphicsUnit.Pixel);

                        //right-top
                        r1 = new Rectangle(x + img.Width / Totalindex - lr.Right, y, lr.Right, lr.Top);
                        r2 = new Rectangle(x1 + r.Width - lr.Right, y1, lr.Right, lr.Top);
                        g.DrawImage(img, r2, r1, GraphicsUnit.Pixel);

                        //Right
                        r1 = new Rectangle(x + img.Width / Totalindex - lr.Right, y + lr.Top,
                            lr.Right, img.Height - lr.Top - lr.Bottom);
                        r2 = new Rectangle(x1 + r.Width - lr.Right, y1 + lr.Top,
                            lr.Right, r.Height - lr.Top - lr.Bottom);
                        g.DrawImage(img, r2, r1, GraphicsUnit.Pixel);

                        //right-bottom
                        r1 = new Rectangle(x + img.Width / Totalindex - lr.Right, y + img.Height - lr.Bottom,
                            lr.Right, lr.Bottom);
                        r2 = new Rectangle(x1 + r.Width - lr.Right, y1 + r.Height - lr.Bottom,
                            lr.Right, lr.Bottom);
                        g.DrawImage(img, r2, r1, GraphicsUnit.Pixel);

                        //bottom
                        r1 = new Rectangle(x + lr.Left, y + img.Height - lr.Bottom,
                            img.Width / Totalindex - lr.Left - lr.Right, lr.Bottom);
                        r2 = new Rectangle(x1 + lr.Left, y1 + r.Height - lr.Bottom,
                            r.Width - lr.Left - lr.Right, lr.Bottom);
                        g.DrawImage(img, r2, r1, GraphicsUnit.Pixel);

                        //Center
                        r1 = new Rectangle(x + lr.Left, y + lr.Top,
                            img.Width / Totalindex - lr.Left - lr.Right, img.Height - lr.Top - lr.Bottom);
                        r2 = new Rectangle(x1 + lr.Left, y1 + lr.Top,
                            r.Width - lr.Left - lr.Right, r.Height - lr.Top - lr.Bottom);
                        g.DrawImage(img, r2, r1, GraphicsUnit.Pixel);
                    }
        }

        /// <summary>
        /// 绘图对像
        /// </summary>
        /// <param name="g"> 绘图对像</param>
        /// <param name="obj">图片对像</param>
        /// <param name="r">绘置的图片大小、坐标</param>
        /// <param name="index">当前状态</param>
        /// <param name="Totalindex">状态总数</param>
        public static void DrawRect(Graphics g, Bitmap img, Rectangle r, int index, int Totalindex)
        {
            if (img == null) return;
            int width = img.Width / Totalindex;
            int height = img.Height;
            Rectangle r1, r2;
            int x = (index - 1) * width;
            int y = 0;
            int x1 = r.Left;
            int y1 = r.Top;
            r1 = new Rectangle(x, y, width, height);
            r2 = new Rectangle(x1, y1, r.Width, r.Height);
            g.DrawImage(img, r2, r1, GraphicsUnit.Pixel);
        }

        public static Rectangle HAlignWithin(Size alignThis, Rectangle withinThis, ContentAlignment align)
        {
            if ((align & anyRight) != (ContentAlignment)0)
            {
                withinThis.X += (withinThis.Width - alignThis.Width);
            }
            else if ((align & anyCenter) != ((ContentAlignment)0))
            {
                withinThis.X += ((withinThis.Width - alignThis.Width + 1) / 2);
            }
            withinThis.Width = alignThis.Width;
            return withinThis;
        }

        public static Rectangle VAlignWithin(Size alignThis, Rectangle withinThis, ContentAlignment align)
        {
            if ((align & anyBottom) != ((ContentAlignment)0))
            {
                withinThis.Y += (withinThis.Height - alignThis.Height);
            }
            else if ((align & anyMiddle) != ((ContentAlignment)0))
            {
                withinThis.Y += ((withinThis.Height - alignThis.Height + 1) / 2);
            }
            withinThis.Height = alignThis.Height;
            return withinThis;
        }

        /// <summary>
        /// 将多个 区域合并成一个区域, 并返回这个 合并区域的 最大值
        /// </summary>
        public static RectangleF MergeRectangleF(params RectangleF[] rects)
        {
            if (rects == null || rects.Length <= 0) return RectangleF.Empty;

            float left = short.MaxValue;
            float top = short.MaxValue;
            float right = 0;
            float bottom = 0;

            foreach(RectangleF rect in rects)
            {
                left = Math.Min(left, rect.X);
                top = Math.Min(top, rect.Y);
                right = Math.Max(right, rect.Right);
                bottom = Math.Max(bottom, rect.Bottom);
            }

            if (left > right) { left = right; }
            if (top > bottom) { top = bottom; }

            return new RectangleF(left, top, (right - left), (bottom - top));
        }

        #endregion


        #region  私 有 函 数


        private static SizeF RotateSize(SizeF size, int angle)
        {
            if (angle == 0 || angle == 180) return size;
            if (angle == 90 || angle == 270) return new SizeF(size.Height, size.Width);

            Matrix matrix = new Matrix();
            matrix.Rotate(angle);

            PointF[] points = new PointF[4]{ new PointF(-size.Width / 2f,  -size.Height / 2f), new PointF(-size.Width / 2f,size.Height / 2f), new PointF(size.Width / 2f,size.Height / 2f ), new PointF(size.Width / 2f, -size.Height / 2f)  };
            matrix.TransformPoints(points);

            // 求取四个顶点的包围盒
            float left = float.MaxValue;
            float right = float.MinValue;
            float top = float.MaxValue;
            float bottom = float.MinValue;

            foreach (PointF p in points)
            {
                if (p.X < left) left = p.X;
                if (p.X > right) right = p.X;
                if (p.Y < top) top = p.Y;
                if (p.Y > bottom) bottom = p.Y;
            }

            SizeF result = new SizeF(right - left, bottom - top);
            return result;
        }
        private static PointF GetRotatePoint(RectangleF rect, StringFormat format)
        {
            PointF p = new PointF();
            var alignH = format.Alignment;
            var alignV = format.LineAlignment;

            if (alignH == StringAlignment.Near) { p.X = rect.Left + rect.Width / 2f; }
            else if (alignH == StringAlignment.Center) { p.X = (rect.Left + rect.Right) / 2f; }
            else if (alignH == StringAlignment.Far) { p.X = rect.Right - rect.Width / 2f; }

            if (alignV == StringAlignment.Near) { p.Y = rect.Top + rect.Height / 2f; }
            else if (alignV == StringAlignment.Center) { p.Y = (rect.Top + rect.Bottom) / 2f; }
            else if (alignV == StringAlignment.Far) { p.Y = rect.Bottom - rect.Height / 2f; }

            return p;
        }

        #endregion



        public static Color CreateDisabledColor(System.Windows.Forms.Control control, Color backColor)
        {
            Color result = backColor;
            if (result.A == 0)
            {
                System.Windows.Forms.Control parentInternal = control.Parent;
                while (result.A == 0)
                {
                    if (parentInternal == null)
                    {
                        result = SystemColors.Control;
                        break;
                    }
                    result = parentInternal.BackColor;
                    parentInternal = parentInternal.Parent;
                }
            }
            return result;
        }


        public static byte[] GetImageBytes(Image img)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                img.Save(ms, ImageFormat.Png);
                return ms.ToArray();
            }
        }
        public static string GetImageBase64(Image img)
        {
            byte[] bytes = GetImageBytes(img);
            string base64 = Convert.ToBase64String(bytes);
            return base64;
        }



    }

    public delegate void DrawRectGraphics(Graphics g, RECT rect);


}
